2 * Copyright © 2017 Intel Corporation
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
24 * @file iris_resource.c
26 * Resources are images, buffers, and other objects used by the GPU.
28 * XXX: explain resources
33 #include "pipe/p_defines.h"
34 #include "pipe/p_state.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "util/os_memory.h"
38 #include "util/u_cpu_detect.h"
39 #include "util/u_inlines.h"
40 #include "util/u_format.h"
41 #include "util/u_transfer.h"
42 #include "util/u_transfer_helper.h"
43 #include "util/u_upload_mgr.h"
44 #include "util/ralloc.h"
45 #include "iris_batch.h"
46 #include "iris_context.h"
47 #include "iris_resource.h"
48 #include "iris_screen.h"
49 #include "intel/common/gen_debug.h"
51 #include "drm-uapi/drm_fourcc.h"
52 #include "drm-uapi/i915_drm.h"
54 enum modifier_priority
{
55 MODIFIER_PRIORITY_INVALID
= 0,
56 MODIFIER_PRIORITY_LINEAR
,
59 MODIFIER_PRIORITY_Y_CCS
,
62 static const uint64_t priority_to_modifier
[] = {
63 [MODIFIER_PRIORITY_INVALID
] = DRM_FORMAT_MOD_INVALID
,
64 [MODIFIER_PRIORITY_LINEAR
] = DRM_FORMAT_MOD_LINEAR
,
65 [MODIFIER_PRIORITY_X
] = I915_FORMAT_MOD_X_TILED
,
66 [MODIFIER_PRIORITY_Y
] = I915_FORMAT_MOD_Y_TILED
,
67 [MODIFIER_PRIORITY_Y_CCS
] = I915_FORMAT_MOD_Y_TILED_CCS
,
71 modifier_is_supported(const struct gen_device_info
*devinfo
,
74 /* XXX: do something real */
76 case I915_FORMAT_MOD_Y_TILED
:
77 case I915_FORMAT_MOD_X_TILED
:
78 case DRM_FORMAT_MOD_LINEAR
:
80 case I915_FORMAT_MOD_Y_TILED_CCS
:
81 case DRM_FORMAT_MOD_INVALID
:
88 select_best_modifier(struct gen_device_info
*devinfo
,
89 const uint64_t *modifiers
,
92 enum modifier_priority prio
= MODIFIER_PRIORITY_INVALID
;
94 for (int i
= 0; i
< count
; i
++) {
95 if (!modifier_is_supported(devinfo
, modifiers
[i
]))
98 switch (modifiers
[i
]) {
99 case I915_FORMAT_MOD_Y_TILED_CCS
:
100 prio
= MAX2(prio
, MODIFIER_PRIORITY_Y_CCS
);
102 case I915_FORMAT_MOD_Y_TILED
:
103 prio
= MAX2(prio
, MODIFIER_PRIORITY_Y
);
105 case I915_FORMAT_MOD_X_TILED
:
106 prio
= MAX2(prio
, MODIFIER_PRIORITY_X
);
108 case DRM_FORMAT_MOD_LINEAR
:
109 prio
= MAX2(prio
, MODIFIER_PRIORITY_LINEAR
);
111 case DRM_FORMAT_MOD_INVALID
:
117 return priority_to_modifier
[prio
];
120 static enum isl_surf_dim
121 target_to_isl_surf_dim(enum pipe_texture_target target
)
125 case PIPE_TEXTURE_1D
:
126 case PIPE_TEXTURE_1D_ARRAY
:
127 return ISL_SURF_DIM_1D
;
128 case PIPE_TEXTURE_2D
:
129 case PIPE_TEXTURE_CUBE
:
130 case PIPE_TEXTURE_RECT
:
131 case PIPE_TEXTURE_2D_ARRAY
:
132 case PIPE_TEXTURE_CUBE_ARRAY
:
133 return ISL_SURF_DIM_2D
;
134 case PIPE_TEXTURE_3D
:
135 return ISL_SURF_DIM_3D
;
136 case PIPE_MAX_TEXTURE_TYPES
:
139 unreachable("invalid texture type");
142 static isl_surf_usage_flags_t
143 pipe_bind_to_isl_usage(unsigned bindings
)
145 isl_surf_usage_flags_t usage
= 0;
147 if (bindings
& PIPE_BIND_RENDER_TARGET
)
148 usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
150 if (bindings
& PIPE_BIND_SAMPLER_VIEW
)
151 usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
153 if (bindings
& (PIPE_BIND_SHADER_IMAGE
| PIPE_BIND_SHADER_BUFFER
))
154 usage
|= ISL_SURF_USAGE_STORAGE_BIT
;
156 if (bindings
& PIPE_BIND_DISPLAY_TARGET
)
157 usage
|= ISL_SURF_USAGE_DISPLAY_BIT
;
162 struct pipe_resource
*
163 iris_resource_get_separate_stencil(struct pipe_resource
*p_res
)
165 /* For packed depth-stencil, we treat depth as the primary resource
166 * and store S8 as the "second plane" resource.
172 iris_resource_set_separate_stencil(struct pipe_resource
*p_res
,
173 struct pipe_resource
*stencil
)
175 assert(util_format_has_depth(util_format_description(p_res
->format
)));
176 pipe_resource_reference(&p_res
->next
, stencil
);
180 iris_get_depth_stencil_resources(struct pipe_resource
*res
,
181 struct iris_resource
**out_z
,
182 struct iris_resource
**out_s
)
190 if (res
->format
!= PIPE_FORMAT_S8_UINT
) {
191 *out_z
= (void *) res
;
192 *out_s
= (void *) iris_resource_get_separate_stencil(res
);
195 *out_s
= (void *) res
;
200 iris_resource_disable_aux(struct iris_resource
*res
)
202 iris_bo_unreference(res
->aux
.bo
);
203 free(res
->aux
.state
);
205 // XXX: clear color BO
208 res
->aux
.usage
= ISL_AUX_USAGE_NONE
;
209 res
->aux
.surf
.size_B
= 0;
211 res
->aux
.state
= NULL
;
215 iris_resource_destroy(struct pipe_screen
*screen
,
216 struct pipe_resource
*resource
)
218 struct iris_resource
*res
= (struct iris_resource
*)resource
;
220 iris_resource_disable_aux(res
);
222 iris_bo_unreference(res
->bo
);
226 static struct iris_resource
*
227 iris_alloc_resource(struct pipe_screen
*pscreen
,
228 const struct pipe_resource
*templ
)
230 struct iris_resource
*res
= calloc(1, sizeof(struct iris_resource
));
235 res
->base
.screen
= pscreen
;
236 pipe_reference_init(&res
->base
.reference
, 1);
238 res
->aux
.possible_usages
= 1 << ISL_AUX_USAGE_NONE
;
244 iris_get_num_logical_layers(const struct iris_resource
*res
, unsigned level
)
246 if (res
->surf
.dim
== ISL_SURF_DIM_3D
)
247 return minify(res
->surf
.logical_level0_px
.depth
, level
);
249 return res
->surf
.logical_level0_px
.array_len
;
252 static enum isl_aux_state
**
253 create_aux_state_map(struct iris_resource
*res
, enum isl_aux_state initial
)
255 uint32_t total_slices
= 0;
256 for (uint32_t level
= 0; level
< res
->surf
.levels
; level
++)
257 total_slices
+= iris_get_num_logical_layers(res
, level
);
259 const size_t per_level_array_size
=
260 res
->surf
.levels
* sizeof(enum isl_aux_state
*);
262 /* We're going to allocate a single chunk of data for both the per-level
263 * reference array and the arrays of aux_state. This makes cleanup
264 * significantly easier.
266 const size_t total_size
=
267 per_level_array_size
+ total_slices
* sizeof(enum isl_aux_state
);
269 void *data
= malloc(total_size
);
273 enum isl_aux_state
**per_level_arr
= data
;
274 enum isl_aux_state
*s
= data
+ per_level_array_size
;
275 for (uint32_t level
= 0; level
< res
->surf
.levels
; level
++) {
276 per_level_arr
[level
] = s
;
277 const unsigned level_layers
= iris_get_num_logical_layers(res
, level
);
278 for (uint32_t a
= 0; a
< level_layers
; a
++)
281 assert((void *)s
== data
+ total_size
);
283 return per_level_arr
;
287 * Allocate the initial aux surface for a resource based on aux.usage
290 iris_resource_alloc_aux(struct iris_screen
*screen
, struct iris_resource
*res
)
292 struct isl_device
*isl_dev
= &screen
->isl_dev
;
293 enum isl_aux_state initial_state
;
294 UNUSED
bool ok
= false;
295 uint8_t memset_value
= 0;
296 uint32_t alloc_flags
= 0;
298 assert(!res
->aux
.bo
);
300 switch (res
->aux
.usage
) {
301 case ISL_AUX_USAGE_NONE
:
302 res
->aux
.surf
.size_B
= 0;
304 case ISL_AUX_USAGE_HIZ
:
305 initial_state
= ISL_AUX_STATE_AUX_INVALID
;
307 ok
= isl_surf_get_hiz_surf(isl_dev
, &res
->surf
, &res
->aux
.surf
);
309 case ISL_AUX_USAGE_MCS
:
310 /* The Ivybridge PRM, Vol 2 Part 1 p326 says:
312 * "When MCS buffer is enabled and bound to MSRT, it is required
313 * that it is cleared prior to any rendering."
315 * Since we only use the MCS buffer for rendering, we just clear it
316 * immediately on allocation. The clear value for MCS buffers is all
317 * 1's, so we simply memset it to 0xff.
319 initial_state
= ISL_AUX_STATE_CLEAR
;
321 ok
= isl_surf_get_mcs_surf(isl_dev
, &res
->surf
, &res
->aux
.surf
);
323 case ISL_AUX_USAGE_CCS_D
:
324 case ISL_AUX_USAGE_CCS_E
:
325 /* When CCS_E is used, we need to ensure that the CCS starts off in
326 * a valid state. From the Sky Lake PRM, "MCS Buffer for Render
329 * "If Software wants to enable Color Compression without Fast
330 * clear, Software needs to initialize MCS with zeros."
332 * A CCS value of 0 indicates that the corresponding block is in the
333 * pass-through state which is what we want.
335 * For CCS_D, do the same thing. On Gen9+, this avoids having any
336 * undefined bits in the aux buffer.
338 initial_state
= ISL_AUX_STATE_PASS_THROUGH
;
339 alloc_flags
|= BO_ALLOC_ZEROED
;
340 ok
= isl_surf_get_ccs_surf(isl_dev
, &res
->surf
, &res
->aux
.surf
, 0);
344 /* No work is needed for a zero-sized auxiliary buffer. */
345 if (res
->aux
.surf
.size_B
== 0)
348 /* Assert that ISL gave us a valid aux surf */
351 /* Create the aux_state for the auxiliary buffer. */
352 res
->aux
.state
= create_aux_state_map(res
, initial_state
);
356 /* Allocate the auxiliary buffer. ISL has stricter set of alignment rules
357 * the drm allocator. Therefore, one can pass the ISL dimensions in terms
358 * of bytes instead of trying to recalculate based on different format
361 res
->aux
.bo
= iris_bo_alloc_tiled(screen
->bufmgr
, "aux buffer",
362 res
->aux
.surf
.size_B
,
363 IRIS_MEMZONE_OTHER
, I915_TILING_Y
,
364 res
->aux
.surf
.row_pitch_B
, alloc_flags
);
368 /* Optionally, initialize the auxiliary data to the desired value. */
369 if (memset_value
!= 0) {
370 void *map
= iris_bo_map(NULL
, res
->aux
.bo
, MAP_WRITE
| MAP_RAW
);
374 memset(map
, memset_value
, res
->aux
.surf
.size_B
);
375 iris_bo_unmap(res
->aux
.bo
);
384 supports_mcs(const struct isl_surf
*surf
)
386 /* MCS compression only applies to multisampled resources. */
387 if (surf
->samples
<= 1)
390 /* See isl_surf_get_mcs_surf for details. */
391 if (surf
->samples
== 16 && surf
->logical_level0_px
.width
> 8192)
394 /* Depth and stencil buffers use the IMS (interleaved) layout. */
395 if (isl_surf_usage_is_depth_or_stencil(surf
->usage
))
402 supports_ccs(const struct gen_device_info
*devinfo
,
403 const struct isl_surf
*surf
)
405 /* Gen9+ only supports CCS for Y-tiled buffers. */
406 if (surf
->tiling
!= ISL_TILING_Y0
)
409 /* CCS only supports singlesampled resources. */
410 if (surf
->samples
> 1)
413 /* The PRM doesn't say this explicitly, but fast-clears don't appear to
414 * work for 3D textures until Gen9 where the layout of 3D textures changes
415 * to match 2D array textures.
417 if (devinfo
->gen
< 9 && surf
->dim
!= ISL_SURF_DIM_2D
)
420 /* Note: still need to check the format! */
425 static struct pipe_resource
*
426 iris_resource_create_for_buffer(struct pipe_screen
*pscreen
,
427 const struct pipe_resource
*templ
)
429 struct iris_screen
*screen
= (struct iris_screen
*)pscreen
;
430 struct iris_resource
*res
= iris_alloc_resource(pscreen
, templ
);
432 assert(templ
->target
== PIPE_BUFFER
);
433 assert(templ
->height0
<= 1);
434 assert(templ
->depth0
<= 1);
435 assert(templ
->format
== PIPE_FORMAT_NONE
||
436 util_format_get_blocksize(templ
->format
) == 1);
438 res
->internal_format
= templ
->format
;
439 res
->surf
.tiling
= ISL_TILING_LINEAR
;
441 enum iris_memory_zone memzone
= IRIS_MEMZONE_OTHER
;
442 const char *name
= templ
->target
== PIPE_BUFFER
? "buffer" : "miptree";
443 if (templ
->flags
& IRIS_RESOURCE_FLAG_SHADER_MEMZONE
) {
444 memzone
= IRIS_MEMZONE_SHADER
;
445 name
= "shader kernels";
446 } else if (templ
->flags
& IRIS_RESOURCE_FLAG_SURFACE_MEMZONE
) {
447 memzone
= IRIS_MEMZONE_SURFACE
;
448 name
= "surface state";
449 } else if (templ
->flags
& IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE
) {
450 memzone
= IRIS_MEMZONE_DYNAMIC
;
451 name
= "dynamic state";
454 res
->bo
= iris_bo_alloc(screen
->bufmgr
, name
, templ
->width0
, memzone
);
456 iris_resource_destroy(pscreen
, &res
->base
);
463 static struct pipe_resource
*
464 iris_resource_create_with_modifiers(struct pipe_screen
*pscreen
,
465 const struct pipe_resource
*templ
,
466 const uint64_t *modifiers
,
469 struct iris_screen
*screen
= (struct iris_screen
*)pscreen
;
470 struct gen_device_info
*devinfo
= &screen
->devinfo
;
471 struct iris_resource
*res
= iris_alloc_resource(pscreen
, templ
);
476 const struct util_format_description
*format_desc
=
477 util_format_description(templ
->format
);
478 const bool has_depth
= util_format_has_depth(format_desc
);
479 const struct isl_drm_modifier_info
*mod_info
= NULL
;
481 select_best_modifier(devinfo
, modifiers
, modifiers_count
);
483 isl_tiling_flags_t tiling_flags
= ISL_TILING_ANY_MASK
;
485 if (modifier
!= DRM_FORMAT_MOD_INVALID
) {
486 mod_info
= isl_drm_modifier_get_info(modifier
);
488 tiling_flags
= 1 << mod_info
->tiling
;
490 if (modifiers_count
> 0) {
491 fprintf(stderr
, "Unsupported modifier, resource creation failed.\n");
495 /* No modifiers - we can select our own tiling. */
498 /* Depth must be Y-tiled */
499 tiling_flags
= ISL_TILING_Y0_BIT
;
500 } else if (templ
->format
== PIPE_FORMAT_S8_UINT
) {
501 /* Stencil must be W-tiled */
502 tiling_flags
= ISL_TILING_W_BIT
;
503 } else if (templ
->target
== PIPE_BUFFER
||
504 templ
->target
== PIPE_TEXTURE_1D
||
505 templ
->target
== PIPE_TEXTURE_1D_ARRAY
) {
506 /* Use linear for buffers and 1D textures */
507 tiling_flags
= ISL_TILING_LINEAR_BIT
;
510 /* Use linear for staging buffers */
511 if (templ
->usage
== PIPE_USAGE_STAGING
||
512 templ
->bind
& (PIPE_BIND_LINEAR
| PIPE_BIND_CURSOR
) )
513 tiling_flags
= ISL_TILING_LINEAR_BIT
;
516 isl_surf_usage_flags_t usage
= pipe_bind_to_isl_usage(templ
->bind
);
518 if (templ
->target
== PIPE_TEXTURE_CUBE
||
519 templ
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
520 usage
|= ISL_SURF_USAGE_CUBE_BIT
;
522 if (templ
->usage
!= PIPE_USAGE_STAGING
) {
523 if (templ
->format
== PIPE_FORMAT_S8_UINT
)
524 usage
|= ISL_SURF_USAGE_STENCIL_BIT
;
526 usage
|= ISL_SURF_USAGE_DEPTH_BIT
;
529 enum pipe_format pfmt
= templ
->format
;
530 res
->internal_format
= pfmt
;
532 /* Should be handled by u_transfer_helper */
533 assert(!util_format_is_depth_and_stencil(pfmt
));
535 struct iris_format_info fmt
= iris_format_for_usage(devinfo
, pfmt
, usage
);
536 assert(fmt
.fmt
!= ISL_FORMAT_UNSUPPORTED
);
538 UNUSED
const bool isl_surf_created_successfully
=
539 isl_surf_init(&screen
->isl_dev
, &res
->surf
,
540 .dim
= target_to_isl_surf_dim(templ
->target
),
542 .width
= templ
->width0
,
543 .height
= templ
->height0
,
544 .depth
= templ
->depth0
,
545 .levels
= templ
->last_level
+ 1,
546 .array_len
= templ
->array_size
,
547 .samples
= MAX2(templ
->nr_samples
, 1),
548 .min_alignment_B
= 0,
551 .tiling_flags
= tiling_flags
);
552 assert(isl_surf_created_successfully
);
555 res
->aux
.possible_usages
|= 1 << mod_info
->aux_usage
;
556 } else if (has_depth
) {
557 res
->aux
.possible_usages
|= 1 << ISL_AUX_USAGE_HIZ
;
558 } else if (supports_mcs(&res
->surf
)) {
559 res
->aux
.possible_usages
|= 1 << ISL_AUX_USAGE_MCS
;
560 } else if (supports_ccs(devinfo
, &res
->surf
)) {
561 if (isl_format_supports_ccs_e(devinfo
, res
->surf
.format
))
562 res
->aux
.possible_usages
|= 1 << ISL_AUX_USAGE_CCS_E
;
563 else if (isl_format_supports_ccs_d(devinfo
, res
->surf
.format
))
564 res
->aux
.possible_usages
|= 1 << ISL_AUX_USAGE_CCS_D
;
567 // XXX: we don't actually do aux yet
568 res
->aux
.possible_usages
= 1 << ISL_AUX_USAGE_NONE
;
570 res
->aux
.usage
= util_last_bit(res
->aux
.possible_usages
) - 1;
572 const char *name
= "miptree";
573 enum iris_memory_zone memzone
= IRIS_MEMZONE_OTHER
;
575 /* These are for u_upload_mgr buffers only */
576 assert(!(templ
->flags
& (IRIS_RESOURCE_FLAG_SHADER_MEMZONE
|
577 IRIS_RESOURCE_FLAG_SURFACE_MEMZONE
|
578 IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE
)));
580 res
->bo
= iris_bo_alloc_tiled(screen
->bufmgr
, name
, res
->surf
.size_B
,
582 isl_tiling_to_i915_tiling(res
->surf
.tiling
),
583 res
->surf
.row_pitch_B
, 0);
588 if (!iris_resource_alloc_aux(screen
, res
))
594 fprintf(stderr
, "XXX: resource creation failed\n");
595 iris_resource_destroy(pscreen
, &res
->base
);
600 static struct pipe_resource
*
601 iris_resource_create(struct pipe_screen
*pscreen
,
602 const struct pipe_resource
*templ
)
604 if (templ
->target
== PIPE_BUFFER
)
605 return iris_resource_create_for_buffer(pscreen
, templ
);
607 return iris_resource_create_with_modifiers(pscreen
, templ
, NULL
, 0);
611 tiling_to_modifier(uint32_t tiling
)
613 static const uint64_t map
[] = {
614 [I915_TILING_NONE
] = DRM_FORMAT_MOD_LINEAR
,
615 [I915_TILING_X
] = I915_FORMAT_MOD_X_TILED
,
616 [I915_TILING_Y
] = I915_FORMAT_MOD_Y_TILED
,
619 assert(tiling
< ARRAY_SIZE(map
));
624 static struct pipe_resource
*
625 iris_resource_from_user_memory(struct pipe_screen
*pscreen
,
626 const struct pipe_resource
*templ
,
629 struct iris_screen
*screen
= (struct iris_screen
*)pscreen
;
630 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
631 struct iris_resource
*res
= iris_alloc_resource(pscreen
, templ
);
635 assert(templ
->target
== PIPE_BUFFER
);
637 res
->internal_format
= templ
->format
;
638 res
->bo
= iris_bo_create_userptr(bufmgr
, "user",
639 user_memory
, templ
->width0
,
649 static struct pipe_resource
*
650 iris_resource_from_handle(struct pipe_screen
*pscreen
,
651 const struct pipe_resource
*templ
,
652 struct winsys_handle
*whandle
,
655 struct iris_screen
*screen
= (struct iris_screen
*)pscreen
;
656 struct gen_device_info
*devinfo
= &screen
->devinfo
;
657 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
658 struct iris_resource
*res
= iris_alloc_resource(pscreen
, templ
);
662 if (whandle
->offset
!= 0) {
663 dbg_printf("Attempt to import unsupported winsys offset %u\n",
668 switch (whandle
->type
) {
669 case WINSYS_HANDLE_TYPE_FD
:
670 res
->bo
= iris_bo_import_dmabuf(bufmgr
, whandle
->handle
);
672 case WINSYS_HANDLE_TYPE_SHARED
:
673 res
->bo
= iris_bo_gem_create_from_name(bufmgr
, "winsys image",
677 unreachable("invalid winsys handle type");
682 uint64_t modifier
= whandle
->modifier
;
683 if (modifier
== DRM_FORMAT_MOD_INVALID
) {
684 modifier
= tiling_to_modifier(res
->bo
->tiling_mode
);
686 const struct isl_drm_modifier_info
*mod_info
=
687 isl_drm_modifier_get_info(modifier
);
690 isl_surf_usage_flags_t isl_usage
= pipe_bind_to_isl_usage(templ
->bind
);
692 const struct iris_format_info fmt
=
693 iris_format_for_usage(devinfo
, templ
->format
, isl_usage
);
694 res
->internal_format
= templ
->format
;
696 if (templ
->target
== PIPE_BUFFER
) {
697 res
->surf
.tiling
= ISL_TILING_LINEAR
;
699 isl_surf_init(&screen
->isl_dev
, &res
->surf
,
700 .dim
= target_to_isl_surf_dim(templ
->target
),
702 .width
= templ
->width0
,
703 .height
= templ
->height0
,
704 .depth
= templ
->depth0
,
705 .levels
= templ
->last_level
+ 1,
706 .array_len
= templ
->array_size
,
707 .samples
= MAX2(templ
->nr_samples
, 1),
708 .min_alignment_B
= 0,
709 .row_pitch_B
= whandle
->stride
,
711 .tiling_flags
= 1 << mod_info
->tiling
);
713 assert(res
->bo
->tiling_mode
==
714 isl_tiling_to_i915_tiling(res
->surf
.tiling
));
716 // XXX: create_ccs_buf_for_image?
717 if (!iris_resource_alloc_aux(screen
, res
))
724 iris_resource_destroy(pscreen
, &res
->base
);
729 iris_resource_get_handle(struct pipe_screen
*pscreen
,
730 struct pipe_context
*ctx
,
731 struct pipe_resource
*resource
,
732 struct winsys_handle
*whandle
,
735 struct iris_resource
*res
= (struct iris_resource
*)resource
;
737 /* If this is a buffer, stride should be 0 - no need to special case */
738 whandle
->stride
= res
->surf
.row_pitch_B
;
739 whandle
->modifier
= tiling_to_modifier(res
->bo
->tiling_mode
);
741 switch (whandle
->type
) {
742 case WINSYS_HANDLE_TYPE_SHARED
:
743 return iris_bo_flink(res
->bo
, &whandle
->handle
) == 0;
744 case WINSYS_HANDLE_TYPE_KMS
:
745 whandle
->handle
= iris_bo_export_gem_handle(res
->bo
);
747 case WINSYS_HANDLE_TYPE_FD
:
748 return iris_bo_export_dmabuf(res
->bo
, (int *) &whandle
->handle
) == 0;
755 get_image_offset_el(struct isl_surf
*surf
, unsigned level
, unsigned z
,
756 unsigned *out_x0_el
, unsigned *out_y0_el
)
758 if (surf
->dim
== ISL_SURF_DIM_3D
) {
759 isl_surf_get_image_offset_el(surf
, level
, 0, z
, out_x0_el
, out_y0_el
);
761 isl_surf_get_image_offset_el(surf
, level
, z
, 0, out_x0_el
, out_y0_el
);
766 * Get pointer offset into stencil buffer.
768 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
769 * must decode the tile's layout in software.
772 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
774 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
776 * Even though the returned offset is always positive, the return type is
778 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
779 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
782 s8_offset(uint32_t stride
, uint32_t x
, uint32_t y
, bool swizzled
)
784 uint32_t tile_size
= 4096;
785 uint32_t tile_width
= 64;
786 uint32_t tile_height
= 64;
787 uint32_t row_size
= 64 * stride
/ 2; /* Two rows are interleaved. */
789 uint32_t tile_x
= x
/ tile_width
;
790 uint32_t tile_y
= y
/ tile_height
;
792 /* The byte's address relative to the tile's base addres. */
793 uint32_t byte_x
= x
% tile_width
;
794 uint32_t byte_y
= y
% tile_height
;
796 uintptr_t u
= tile_y
* row_size
800 + 32 * ((byte_y
/ 4) % 2)
801 + 16 * ((byte_x
/ 4) % 2)
802 + 8 * ((byte_y
/ 2) % 2)
803 + 4 * ((byte_x
/ 2) % 2)
808 /* adjust for bit6 swizzling */
809 if (((byte_x
/ 8) % 2) == 1) {
810 if (((byte_y
/ 8) % 2) == 0) {
822 iris_unmap_s8(struct iris_transfer
*map
)
824 struct pipe_transfer
*xfer
= &map
->base
;
825 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
826 struct isl_surf
*surf
= &res
->surf
;
827 const bool has_swizzling
= false;
829 if (xfer
->usage
& PIPE_TRANSFER_WRITE
) {
830 uint8_t *untiled_s8_map
= map
->ptr
;
831 uint8_t *tiled_s8_map
=
832 iris_bo_map(map
->dbg
, res
->bo
, xfer
->usage
| MAP_RAW
);
834 struct pipe_box box
= xfer
->box
;
836 for (int s
= 0; s
< box
.depth
; s
++) {
837 unsigned x0_el
, y0_el
;
838 get_image_offset_el(surf
, xfer
->level
, box
.z
, &x0_el
, &y0_el
);
840 for (uint32_t y
= 0; y
< box
.height
; y
++) {
841 for (uint32_t x
= 0; x
< box
.width
; x
++) {
842 ptrdiff_t offset
= s8_offset(surf
->row_pitch_B
,
846 tiled_s8_map
[offset
] =
847 untiled_s8_map
[s
* xfer
->layer_stride
+ y
* xfer
->stride
+ x
];
859 iris_map_s8(struct iris_transfer
*map
)
861 struct pipe_transfer
*xfer
= &map
->base
;
862 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
863 struct isl_surf
*surf
= &res
->surf
;
865 xfer
->stride
= surf
->row_pitch_B
;
866 xfer
->layer_stride
= xfer
->stride
* xfer
->box
.height
;
868 /* The tiling and detiling functions require that the linear buffer has
869 * a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we
870 * over-allocate the linear buffer to get the proper alignment.
872 map
->buffer
= map
->ptr
= malloc(xfer
->layer_stride
* xfer
->box
.depth
);
875 const bool has_swizzling
= false;
877 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
878 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
879 * invalidate is set, since we'll be writing the whole rectangle from our
880 * temporary buffer back out.
882 if (!(xfer
->usage
& PIPE_TRANSFER_DISCARD_RANGE
)) {
883 uint8_t *untiled_s8_map
= map
->ptr
;
884 uint8_t *tiled_s8_map
=
885 iris_bo_map(map
->dbg
, res
->bo
, xfer
->usage
| MAP_RAW
);
887 struct pipe_box box
= xfer
->box
;
889 for (int s
= 0; s
< box
.depth
; s
++) {
890 unsigned x0_el
, y0_el
;
891 get_image_offset_el(surf
, xfer
->level
, box
.z
, &x0_el
, &y0_el
);
893 for (uint32_t y
= 0; y
< box
.height
; y
++) {
894 for (uint32_t x
= 0; x
< box
.width
; x
++) {
895 ptrdiff_t offset
= s8_offset(surf
->row_pitch_B
,
899 untiled_s8_map
[s
* xfer
->layer_stride
+ y
* xfer
->stride
+ x
] =
900 tiled_s8_map
[offset
];
908 map
->unmap
= iris_unmap_s8
;
911 /* Compute extent parameters for use with tiled_memcpy functions.
912 * xs are in units of bytes and ys are in units of strides.
915 tile_extents(struct isl_surf
*surf
,
916 const struct pipe_box
*box
,
918 unsigned *x1_B
, unsigned *x2_B
,
919 unsigned *y1_el
, unsigned *y2_el
)
921 const struct isl_format_layout
*fmtl
= isl_format_get_layout(surf
->format
);
922 const unsigned cpp
= fmtl
->bpb
/ 8;
924 assert(box
->x
% fmtl
->bw
== 0);
925 assert(box
->y
% fmtl
->bh
== 0);
927 unsigned x0_el
, y0_el
;
928 get_image_offset_el(surf
, level
, box
->z
, &x0_el
, &y0_el
);
930 *x1_B
= (box
->x
/ fmtl
->bw
+ x0_el
) * cpp
;
931 *y1_el
= box
->y
/ fmtl
->bh
+ y0_el
;
932 *x2_B
= (DIV_ROUND_UP(box
->x
+ box
->width
, fmtl
->bw
) + x0_el
) * cpp
;
933 *y2_el
= DIV_ROUND_UP(box
->y
+ box
->height
, fmtl
->bh
) + y0_el
;
937 iris_unmap_tiled_memcpy(struct iris_transfer
*map
)
939 struct pipe_transfer
*xfer
= &map
->base
;
940 struct pipe_box box
= xfer
->box
;
941 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
942 struct isl_surf
*surf
= &res
->surf
;
944 const bool has_swizzling
= false;
946 if (xfer
->usage
& PIPE_TRANSFER_WRITE
) {
947 char *dst
= iris_bo_map(map
->dbg
, res
->bo
, xfer
->usage
| MAP_RAW
);
949 for (int s
= 0; s
< box
.depth
; s
++) {
950 unsigned x1
, x2
, y1
, y2
;
951 tile_extents(surf
, &box
, xfer
->level
, &x1
, &x2
, &y1
, &y2
);
953 void *ptr
= map
->ptr
+ s
* xfer
->layer_stride
;
955 isl_memcpy_linear_to_tiled(x1
, x2
, y1
, y2
, dst
, ptr
,
956 surf
->row_pitch_B
, xfer
->stride
,
957 has_swizzling
, surf
->tiling
, ISL_MEMCPY
);
961 os_free_aligned(map
->buffer
);
962 map
->buffer
= map
->ptr
= NULL
;
966 iris_map_tiled_memcpy(struct iris_transfer
*map
)
968 struct pipe_transfer
*xfer
= &map
->base
;
969 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
970 struct isl_surf
*surf
= &res
->surf
;
972 xfer
->stride
= ALIGN(surf
->row_pitch_B
, 16);
973 xfer
->layer_stride
= xfer
->stride
* xfer
->box
.height
;
975 unsigned x1
, x2
, y1
, y2
;
976 tile_extents(surf
, &xfer
->box
, xfer
->level
, &x1
, &x2
, &y1
, &y2
);
978 /* The tiling and detiling functions require that the linear buffer has
979 * a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we
980 * over-allocate the linear buffer to get the proper alignment.
983 os_malloc_aligned(xfer
->layer_stride
* xfer
->box
.depth
, 16);
985 map
->ptr
= (char *)map
->buffer
+ (x1
& 0xf);
987 const bool has_swizzling
= false;
989 // XXX: PIPE_TRANSFER_READ?
990 if (!(xfer
->usage
& PIPE_TRANSFER_DISCARD_RANGE
)) {
991 char *src
= iris_bo_map(map
->dbg
, res
->bo
, xfer
->usage
| MAP_RAW
);
993 struct pipe_box box
= xfer
->box
;
995 for (int s
= 0; s
< box
.depth
; s
++) {
996 unsigned x1
, x2
, y1
, y2
;
997 tile_extents(surf
, &box
, xfer
->level
, &x1
, &x2
, &y1
, &y2
);
999 /* Use 's' rather than 'box.z' to rebase the first slice to 0. */
1000 void *ptr
= map
->ptr
+ s
* xfer
->layer_stride
;
1002 isl_memcpy_tiled_to_linear(x1
, x2
, y1
, y2
, ptr
, src
, xfer
->stride
,
1003 surf
->row_pitch_B
, has_swizzling
,
1004 surf
->tiling
, ISL_MEMCPY
);
1009 map
->unmap
= iris_unmap_tiled_memcpy
;
1013 iris_map_direct(struct iris_transfer
*map
)
1015 struct pipe_transfer
*xfer
= &map
->base
;
1016 struct pipe_box
*box
= &xfer
->box
;
1017 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
1019 void *ptr
= iris_bo_map(map
->dbg
, res
->bo
, xfer
->usage
);
1021 if (res
->base
.target
== PIPE_BUFFER
) {
1023 xfer
->layer_stride
= 0;
1025 map
->ptr
= ptr
+ box
->x
;
1027 struct isl_surf
*surf
= &res
->surf
;
1028 const struct isl_format_layout
*fmtl
=
1029 isl_format_get_layout(surf
->format
);
1030 const unsigned cpp
= fmtl
->bpb
/ 8;
1031 unsigned x0_el
, y0_el
;
1033 get_image_offset_el(surf
, xfer
->level
, box
->z
, &x0_el
, &y0_el
);
1035 xfer
->stride
= isl_surf_get_row_pitch_B(surf
);
1036 xfer
->layer_stride
= isl_surf_get_array_pitch(surf
);
1038 map
->ptr
= ptr
+ (y0_el
+ box
->y
) * xfer
->stride
+ (x0_el
+ box
->x
) * cpp
;
1043 iris_transfer_map(struct pipe_context
*ctx
,
1044 struct pipe_resource
*resource
,
1046 enum pipe_transfer_usage usage
,
1047 const struct pipe_box
*box
,
1048 struct pipe_transfer
**ptransfer
)
1050 struct iris_context
*ice
= (struct iris_context
*)ctx
;
1051 struct iris_resource
*res
= (struct iris_resource
*)resource
;
1052 struct isl_surf
*surf
= &res
->surf
;
1054 /* If we can discard the whole resource, we can also discard the
1055 * subrange being accessed.
1057 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
)
1058 usage
|= PIPE_TRANSFER_DISCARD_RANGE
;
1060 if (surf
->tiling
!= ISL_TILING_LINEAR
&&
1061 (usage
& PIPE_TRANSFER_MAP_DIRECTLY
))
1064 if (resource
->target
!= PIPE_BUFFER
) {
1065 iris_resource_access_raw(ice
, &ice
->batches
[IRIS_BATCH_RENDER
], res
,
1066 level
, box
->z
, box
->depth
,
1067 usage
& PIPE_TRANSFER_WRITE
);
1070 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
1071 for (int i
= 0; i
< IRIS_BATCH_COUNT
; i
++) {
1072 if (iris_batch_references(&ice
->batches
[i
], res
->bo
))
1073 iris_batch_flush(&ice
->batches
[i
]);
1077 if ((usage
& PIPE_TRANSFER_DONTBLOCK
) && iris_bo_busy(res
->bo
))
1080 struct iris_transfer
*map
= slab_alloc(&ice
->transfer_pool
);
1081 struct pipe_transfer
*xfer
= &map
->base
;
1086 memset(map
, 0, sizeof(*map
));
1087 map
->dbg
= &ice
->dbg
;
1089 pipe_resource_reference(&xfer
->resource
, resource
);
1090 xfer
->level
= level
;
1091 xfer
->usage
= usage
;
1095 xfer
->usage
&= (PIPE_TRANSFER_READ
|
1096 PIPE_TRANSFER_WRITE
|
1097 PIPE_TRANSFER_UNSYNCHRONIZED
|
1098 PIPE_TRANSFER_PERSISTENT
|
1099 PIPE_TRANSFER_COHERENT
|
1100 PIPE_TRANSFER_DISCARD_RANGE
);
1102 if (surf
->tiling
== ISL_TILING_W
) {
1103 // XXX: just teach iris_map_tiled_memcpy about W tiling...
1105 } else if (surf
->tiling
!= ISL_TILING_LINEAR
) {
1106 iris_map_tiled_memcpy(map
);
1108 iris_map_direct(map
);
1115 iris_transfer_flush_region(struct pipe_context
*ctx
,
1116 struct pipe_transfer
*xfer
,
1117 const struct pipe_box
*box
)
1119 struct iris_context
*ice
= (struct iris_context
*)ctx
;
1120 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
1123 // XXX: don't emit flushes in both engines...? we may also need to flush
1124 // even if there isn't a draw yet - may still be stale data in caches...
1125 for (int i
= 0; i
< IRIS_BATCH_COUNT
; i
++) {
1126 if (ice
->batches
[i
].contains_draw
) {
1127 iris_batch_maybe_flush(&ice
->batches
[i
], 24);
1128 iris_flush_and_dirty_for_history(ice
, &ice
->batches
[i
], res
);
1134 iris_transfer_unmap(struct pipe_context
*ctx
, struct pipe_transfer
*xfer
)
1136 struct iris_context
*ice
= (struct iris_context
*)ctx
;
1137 struct iris_transfer
*map
= (void *) xfer
;
1138 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
1143 // XXX: don't emit flushes in both engines...?
1144 for (int i
= 0; i
< IRIS_BATCH_COUNT
; i
++) {
1145 if (ice
->batches
[i
].contains_draw
) {
1146 iris_batch_maybe_flush(&ice
->batches
[i
], 24);
1147 iris_flush_and_dirty_for_history(ice
, &ice
->batches
[i
], res
);
1151 pipe_resource_reference(&xfer
->resource
, NULL
);
1152 slab_free(&ice
->transfer_pool
, map
);
1156 iris_flush_resource(struct pipe_context
*ctx
, struct pipe_resource
*resource
)
1161 iris_flush_and_dirty_for_history(struct iris_context
*ice
,
1162 struct iris_batch
*batch
,
1163 struct iris_resource
*res
)
1165 if (res
->base
.target
!= PIPE_BUFFER
)
1168 unsigned flush
= PIPE_CONTROL_CS_STALL
;
1170 /* We've likely used the rendering engine (i.e. BLORP) to write to this
1171 * surface. Flush the render cache so the data actually lands.
1173 if (batch
->name
!= IRIS_BATCH_COMPUTE
)
1174 flush
|= PIPE_CONTROL_RENDER_TARGET_FLUSH
;
1176 uint64_t dirty
= 0ull;
1178 if (res
->bind_history
& PIPE_BIND_CONSTANT_BUFFER
) {
1179 flush
|= PIPE_CONTROL_CONST_CACHE_INVALIDATE
|
1180 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
;
1181 dirty
|= IRIS_DIRTY_CONSTANTS_VS
|
1182 IRIS_DIRTY_CONSTANTS_TCS
|
1183 IRIS_DIRTY_CONSTANTS_TES
|
1184 IRIS_DIRTY_CONSTANTS_GS
|
1185 IRIS_DIRTY_CONSTANTS_FS
|
1186 IRIS_DIRTY_CONSTANTS_CS
|
1187 IRIS_ALL_DIRTY_BINDINGS
;
1190 if (res
->bind_history
& PIPE_BIND_SAMPLER_VIEW
)
1191 flush
|= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
;
1193 if (res
->bind_history
& (PIPE_BIND_VERTEX_BUFFER
| PIPE_BIND_INDEX_BUFFER
))
1194 flush
|= PIPE_CONTROL_VF_CACHE_INVALIDATE
;
1196 if (res
->bind_history
& (PIPE_BIND_SHADER_BUFFER
| PIPE_BIND_SHADER_IMAGE
))
1197 flush
|= PIPE_CONTROL_DATA_CACHE_FLUSH
;
1199 iris_emit_pipe_control_flush(batch
, flush
);
1201 ice
->state
.dirty
|= dirty
;
1204 static enum pipe_format
1205 iris_resource_get_internal_format(struct pipe_resource
*p_res
)
1207 struct iris_resource
*res
= (void *) p_res
;
1208 return res
->internal_format
;
1211 static const struct u_transfer_vtbl transfer_vtbl
= {
1212 .resource_create
= iris_resource_create
,
1213 .resource_destroy
= iris_resource_destroy
,
1214 .transfer_map
= iris_transfer_map
,
1215 .transfer_unmap
= iris_transfer_unmap
,
1216 .transfer_flush_region
= iris_transfer_flush_region
,
1217 .get_internal_format
= iris_resource_get_internal_format
,
1218 .set_stencil
= iris_resource_set_separate_stencil
,
1219 .get_stencil
= iris_resource_get_separate_stencil
,
1223 iris_init_screen_resource_functions(struct pipe_screen
*pscreen
)
1225 pscreen
->resource_create_with_modifiers
=
1226 iris_resource_create_with_modifiers
;
1227 pscreen
->resource_create
= u_transfer_helper_resource_create
;
1228 pscreen
->resource_from_user_memory
= iris_resource_from_user_memory
;
1229 pscreen
->resource_from_handle
= iris_resource_from_handle
;
1230 pscreen
->resource_get_handle
= iris_resource_get_handle
;
1231 pscreen
->resource_destroy
= u_transfer_helper_resource_destroy
;
1232 pscreen
->transfer_helper
=
1233 u_transfer_helper_create(&transfer_vtbl
, true, true, false, true);
1237 iris_init_resource_functions(struct pipe_context
*ctx
)
1239 ctx
->flush_resource
= iris_flush_resource
;
1240 ctx
->transfer_map
= u_transfer_helper_transfer_map
;
1241 ctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
1242 ctx
->transfer_unmap
= u_transfer_helper_transfer_unmap
;
1243 ctx
->buffer_subdata
= u_default_buffer_subdata
;
1244 ctx
->texture_subdata
= u_default_texture_subdata
;