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 // XXX: u_transfer_helper...for separate stencil...
56 enum modifier_priority
{
57 MODIFIER_PRIORITY_INVALID
= 0,
58 MODIFIER_PRIORITY_LINEAR
,
61 MODIFIER_PRIORITY_Y_CCS
,
64 static const uint64_t priority_to_modifier
[] = {
65 [MODIFIER_PRIORITY_INVALID
] = DRM_FORMAT_MOD_INVALID
,
66 [MODIFIER_PRIORITY_LINEAR
] = DRM_FORMAT_MOD_LINEAR
,
67 [MODIFIER_PRIORITY_X
] = I915_FORMAT_MOD_X_TILED
,
68 [MODIFIER_PRIORITY_Y
] = I915_FORMAT_MOD_Y_TILED
,
69 [MODIFIER_PRIORITY_Y_CCS
] = I915_FORMAT_MOD_Y_TILED_CCS
,
73 modifier_is_supported(const struct gen_device_info
*devinfo
,
76 /* XXX: do something real */
78 case I915_FORMAT_MOD_Y_TILED
:
79 case I915_FORMAT_MOD_X_TILED
:
80 case DRM_FORMAT_MOD_LINEAR
:
82 case I915_FORMAT_MOD_Y_TILED_CCS
:
83 case DRM_FORMAT_MOD_INVALID
:
90 select_best_modifier(struct gen_device_info
*devinfo
,
91 const uint64_t *modifiers
,
94 enum modifier_priority prio
= MODIFIER_PRIORITY_INVALID
;
96 for (int i
= 0; i
< count
; i
++) {
97 if (!modifier_is_supported(devinfo
, modifiers
[i
]))
100 switch (modifiers
[i
]) {
101 case I915_FORMAT_MOD_Y_TILED_CCS
:
102 prio
= MAX2(prio
, MODIFIER_PRIORITY_Y_CCS
);
104 case I915_FORMAT_MOD_Y_TILED
:
105 prio
= MAX2(prio
, MODIFIER_PRIORITY_Y
);
107 case I915_FORMAT_MOD_X_TILED
:
108 prio
= MAX2(prio
, MODIFIER_PRIORITY_X
);
110 case DRM_FORMAT_MOD_LINEAR
:
111 prio
= MAX2(prio
, MODIFIER_PRIORITY_LINEAR
);
113 case DRM_FORMAT_MOD_INVALID
:
119 return priority_to_modifier
[prio
];
122 static enum isl_surf_dim
123 target_to_isl_surf_dim(enum pipe_texture_target target
)
127 case PIPE_TEXTURE_1D
:
128 case PIPE_TEXTURE_1D_ARRAY
:
129 return ISL_SURF_DIM_1D
;
130 case PIPE_TEXTURE_2D
:
131 case PIPE_TEXTURE_CUBE
:
132 case PIPE_TEXTURE_RECT
:
133 case PIPE_TEXTURE_2D_ARRAY
:
134 case PIPE_TEXTURE_CUBE_ARRAY
:
135 return ISL_SURF_DIM_2D
;
136 case PIPE_TEXTURE_3D
:
137 return ISL_SURF_DIM_3D
;
138 case PIPE_MAX_TEXTURE_TYPES
:
141 unreachable("invalid texture type");
144 static isl_surf_usage_flags_t
145 pipe_bind_to_isl_usage(unsigned bindings
)
147 isl_surf_usage_flags_t usage
= 0;
149 if (bindings
& PIPE_BIND_RENDER_TARGET
)
150 usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
152 if (bindings
& PIPE_BIND_SAMPLER_VIEW
)
153 usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
155 if (bindings
& (PIPE_BIND_SHADER_IMAGE
| PIPE_BIND_SHADER_BUFFER
))
156 usage
|= ISL_SURF_USAGE_STORAGE_BIT
;
158 if (bindings
& PIPE_BIND_DISPLAY_TARGET
)
159 usage
|= ISL_SURF_USAGE_DISPLAY_BIT
;
164 struct pipe_resource
*
165 iris_resource_get_separate_stencil(struct pipe_resource
*p_res
)
167 /* For packed depth-stencil, we treat depth as the primary resource
168 * and store S8 as the "second plane" resource.
174 iris_resource_set_separate_stencil(struct pipe_resource
*p_res
,
175 struct pipe_resource
*stencil
)
177 assert(util_format_has_depth(util_format_description(p_res
->format
)));
178 pipe_resource_reference(&p_res
->next
, stencil
);
182 iris_get_depth_stencil_resources(struct pipe_resource
*res
,
183 struct iris_resource
**out_z
,
184 struct iris_resource
**out_s
)
192 const struct util_format_description
*desc
=
193 util_format_description(res
->format
);
195 if (util_format_has_depth(desc
)) {
196 *out_z
= (void *) res
;
197 *out_s
= (void *) iris_resource_get_separate_stencil(res
);
199 assert(util_format_has_stencil(desc
));
201 *out_s
= (void *) res
;
206 iris_resource_destroy(struct pipe_screen
*screen
,
207 struct pipe_resource
*resource
)
209 struct iris_resource
*res
= (struct iris_resource
*)resource
;
211 iris_bo_unreference(res
->bo
);
215 static struct iris_resource
*
216 iris_alloc_resource(struct pipe_screen
*pscreen
,
217 const struct pipe_resource
*templ
)
219 struct iris_resource
*res
= calloc(1, sizeof(struct iris_resource
));
224 res
->base
.screen
= pscreen
;
225 pipe_reference_init(&res
->base
.reference
, 1);
230 static struct pipe_resource
*
231 iris_resource_create_with_modifiers(struct pipe_screen
*pscreen
,
232 const struct pipe_resource
*templ
,
233 const uint64_t *modifiers
,
236 struct iris_screen
*screen
= (struct iris_screen
*)pscreen
;
237 struct gen_device_info
*devinfo
= &screen
->devinfo
;
238 struct iris_resource
*res
= iris_alloc_resource(pscreen
, templ
);
239 const struct util_format_description
*format_desc
=
240 util_format_description(templ
->format
);
245 const bool has_depth
= util_format_has_depth(format_desc
);
246 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
248 if (modifiers_count
== 0 || !modifiers
) {
250 modifier
= I915_FORMAT_MOD_Y_TILED
;
251 } else if (templ
->target
== PIPE_TEXTURE_1D
||
252 templ
->target
== PIPE_TEXTURE_1D_ARRAY
) {
253 modifier
= DRM_FORMAT_MOD_LINEAR
;
254 } else if (templ
->bind
& PIPE_BIND_DISPLAY_TARGET
) {
255 /* Display is X-tiled for historical reasons. */
256 modifier
= I915_FORMAT_MOD_X_TILED
;
258 modifier
= I915_FORMAT_MOD_Y_TILED
;
260 /* XXX: make sure this doesn't do stupid things for internal textures */
263 if (templ
->target
== PIPE_BUFFER
|| templ
->usage
== PIPE_USAGE_STAGING
)
264 modifier
= DRM_FORMAT_MOD_LINEAR
;
266 if (templ
->bind
& (PIPE_BIND_LINEAR
| PIPE_BIND_CURSOR
))
267 modifier
= DRM_FORMAT_MOD_LINEAR
;
269 if (modifier
== DRM_FORMAT_MOD_INVALID
) {
270 /* User requested specific modifiers */
271 modifier
= select_best_modifier(devinfo
, modifiers
, modifiers_count
);
272 if (modifier
== DRM_FORMAT_MOD_INVALID
)
276 const struct isl_drm_modifier_info
*mod_info
=
277 isl_drm_modifier_get_info(modifier
);
279 enum isl_tiling tiling
= templ
->format
== PIPE_FORMAT_S8_UINT
?
280 ISL_TILING_W
: mod_info
->tiling
;
282 isl_surf_usage_flags_t usage
= pipe_bind_to_isl_usage(templ
->bind
);
284 if (templ
->target
== PIPE_TEXTURE_CUBE
||
285 templ
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
286 usage
|= ISL_SURF_USAGE_CUBE_BIT
;
288 if (templ
->usage
!= PIPE_USAGE_STAGING
) {
289 if (templ
->format
== PIPE_FORMAT_S8_UINT
)
290 usage
|= ISL_SURF_USAGE_STENCIL_BIT
;
292 usage
|= ISL_SURF_USAGE_DEPTH_BIT
;
295 enum pipe_format pfmt
= templ
->format
;
296 res
->internal_format
= pfmt
;
298 /* Should be handled by u_transfer_helper */
299 assert(!util_format_is_depth_and_stencil(pfmt
));
301 struct iris_format_info fmt
= iris_format_for_usage(devinfo
, pfmt
, usage
);
302 assert(fmt
.fmt
!= ISL_FORMAT_UNSUPPORTED
);
304 UNUSED
const bool isl_surf_created_successfully
=
305 isl_surf_init(&screen
->isl_dev
, &res
->surf
,
306 .dim
= target_to_isl_surf_dim(templ
->target
),
308 .width
= templ
->width0
,
309 .height
= templ
->height0
,
310 .depth
= templ
->depth0
,
311 .levels
= templ
->last_level
+ 1,
312 .array_len
= templ
->array_size
,
313 .samples
= MAX2(templ
->nr_samples
, 1),
314 .min_alignment_B
= 0,
317 .tiling_flags
= 1 << tiling
);
318 assert(isl_surf_created_successfully
);
320 enum iris_memory_zone memzone
= IRIS_MEMZONE_OTHER
;
321 const char *name
= templ
->target
== PIPE_BUFFER
? "buffer" : "miptree";
322 if (templ
->flags
& IRIS_RESOURCE_FLAG_SHADER_MEMZONE
) {
323 memzone
= IRIS_MEMZONE_SHADER
;
324 name
= "shader kernels";
325 } else if (templ
->flags
& IRIS_RESOURCE_FLAG_SURFACE_MEMZONE
) {
326 memzone
= IRIS_MEMZONE_SURFACE
;
327 name
= "surface state";
328 } else if (templ
->flags
& IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE
) {
329 memzone
= IRIS_MEMZONE_DYNAMIC
;
330 name
= "dynamic state";
333 res
->bo
= iris_bo_alloc_tiled(screen
->bufmgr
, name
, res
->surf
.size_B
,
335 isl_tiling_to_i915_tiling(res
->surf
.tiling
),
336 res
->surf
.row_pitch_B
, 0);
343 iris_resource_destroy(pscreen
, &res
->base
);
347 static struct pipe_resource
*
348 iris_resource_create(struct pipe_screen
*pscreen
,
349 const struct pipe_resource
*templ
)
351 return iris_resource_create_with_modifiers(pscreen
, templ
, NULL
, 0);
355 tiling_to_modifier(uint32_t tiling
)
357 static const uint64_t map
[] = {
358 [I915_TILING_NONE
] = DRM_FORMAT_MOD_LINEAR
,
359 [I915_TILING_X
] = I915_FORMAT_MOD_X_TILED
,
360 [I915_TILING_Y
] = I915_FORMAT_MOD_Y_TILED
,
363 assert(tiling
< ARRAY_SIZE(map
));
368 static struct pipe_resource
*
369 iris_resource_from_user_memory(struct pipe_screen
*pscreen
,
370 const struct pipe_resource
*templ
,
373 struct iris_screen
*screen
= (struct iris_screen
*)pscreen
;
374 struct gen_device_info
*devinfo
= &screen
->devinfo
;
375 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
376 struct iris_resource
*res
= iris_alloc_resource(pscreen
, templ
);
380 res
->bo
= iris_bo_create_userptr(bufmgr
, "user",
381 user_memory
, templ
->width0
,
388 res
->internal_format
= templ
->format
;
391 isl_surf_usage_flags_t isl_usage
= 0;
393 const struct iris_format_info fmt
=
394 iris_format_for_usage(devinfo
, templ
->format
, isl_usage
);
396 isl_surf_init(&screen
->isl_dev
, &res
->surf
,
397 .dim
= target_to_isl_surf_dim(templ
->target
),
399 .width
= templ
->width0
,
400 .height
= templ
->height0
,
401 .depth
= templ
->depth0
,
402 .levels
= templ
->last_level
+ 1,
403 .array_len
= templ
->array_size
,
404 .samples
= MAX2(templ
->nr_samples
, 1),
405 .min_alignment_B
= 0,
408 .tiling_flags
= 1 << ISL_TILING_LINEAR
);
410 assert(res
->bo
->tiling_mode
== isl_tiling_to_i915_tiling(res
->surf
.tiling
));
415 static struct pipe_resource
*
416 iris_resource_from_handle(struct pipe_screen
*pscreen
,
417 const struct pipe_resource
*templ
,
418 struct winsys_handle
*whandle
,
421 struct iris_screen
*screen
= (struct iris_screen
*)pscreen
;
422 struct gen_device_info
*devinfo
= &screen
->devinfo
;
423 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
424 struct iris_resource
*res
= iris_alloc_resource(pscreen
, templ
);
428 if (whandle
->offset
!= 0) {
429 dbg_printf("Attempt to import unsupported winsys offset %u\n",
434 switch (whandle
->type
) {
435 case WINSYS_HANDLE_TYPE_FD
:
436 res
->bo
= iris_bo_import_dmabuf(bufmgr
, whandle
->handle
);
438 case WINSYS_HANDLE_TYPE_SHARED
:
439 res
->bo
= iris_bo_gem_create_from_name(bufmgr
, "winsys image",
443 unreachable("invalid winsys handle type");
448 uint64_t modifier
= whandle
->modifier
;
449 if (modifier
== DRM_FORMAT_MOD_INVALID
) {
450 modifier
= tiling_to_modifier(res
->bo
->tiling_mode
);
452 const struct isl_drm_modifier_info
*mod_info
=
453 isl_drm_modifier_get_info(modifier
);
457 isl_surf_usage_flags_t isl_usage
= ISL_SURF_USAGE_DISPLAY_BIT
;
459 const struct iris_format_info fmt
=
460 iris_format_for_usage(devinfo
, templ
->format
, isl_usage
);
462 isl_surf_init(&screen
->isl_dev
, &res
->surf
,
463 .dim
= target_to_isl_surf_dim(templ
->target
),
465 .width
= templ
->width0
,
466 .height
= templ
->height0
,
467 .depth
= templ
->depth0
,
468 .levels
= templ
->last_level
+ 1,
469 .array_len
= templ
->array_size
,
470 .samples
= MAX2(templ
->nr_samples
, 1),
471 .min_alignment_B
= 0,
474 .tiling_flags
= 1 << mod_info
->tiling
);
476 assert(res
->bo
->tiling_mode
== isl_tiling_to_i915_tiling(res
->surf
.tiling
));
481 iris_resource_destroy(pscreen
, &res
->base
);
486 iris_resource_get_handle(struct pipe_screen
*pscreen
,
487 struct pipe_context
*ctx
,
488 struct pipe_resource
*resource
,
489 struct winsys_handle
*whandle
,
492 struct iris_resource
*res
= (struct iris_resource
*)resource
;
494 whandle
->stride
= res
->surf
.row_pitch_B
;
495 whandle
->modifier
= tiling_to_modifier(res
->bo
->tiling_mode
);
497 switch (whandle
->type
) {
498 case WINSYS_HANDLE_TYPE_SHARED
:
499 return iris_bo_flink(res
->bo
, &whandle
->handle
) == 0;
500 case WINSYS_HANDLE_TYPE_KMS
:
501 whandle
->handle
= iris_bo_export_gem_handle(res
->bo
);
503 case WINSYS_HANDLE_TYPE_FD
:
504 return iris_bo_export_dmabuf(res
->bo
, (int *) &whandle
->handle
) == 0;
511 get_image_offset_el(struct isl_surf
*surf
, unsigned level
, unsigned z
,
512 unsigned *out_x0_el
, unsigned *out_y0_el
)
514 if (surf
->dim
== ISL_SURF_DIM_3D
) {
515 isl_surf_get_image_offset_el(surf
, level
, 0, z
, out_x0_el
, out_y0_el
);
517 isl_surf_get_image_offset_el(surf
, level
, z
, 0, out_x0_el
, out_y0_el
);
522 * Get pointer offset into stencil buffer.
524 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
525 * must decode the tile's layout in software.
528 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
530 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
532 * Even though the returned offset is always positive, the return type is
534 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
535 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
538 s8_offset(uint32_t stride
, uint32_t x
, uint32_t y
, bool swizzled
)
540 uint32_t tile_size
= 4096;
541 uint32_t tile_width
= 64;
542 uint32_t tile_height
= 64;
543 uint32_t row_size
= 64 * stride
/ 2; /* Two rows are interleaved. */
545 uint32_t tile_x
= x
/ tile_width
;
546 uint32_t tile_y
= y
/ tile_height
;
548 /* The byte's address relative to the tile's base addres. */
549 uint32_t byte_x
= x
% tile_width
;
550 uint32_t byte_y
= y
% tile_height
;
552 uintptr_t u
= tile_y
* row_size
556 + 32 * ((byte_y
/ 4) % 2)
557 + 16 * ((byte_x
/ 4) % 2)
558 + 8 * ((byte_y
/ 2) % 2)
559 + 4 * ((byte_x
/ 2) % 2)
564 /* adjust for bit6 swizzling */
565 if (((byte_x
/ 8) % 2) == 1) {
566 if (((byte_y
/ 8) % 2) == 0) {
578 iris_unmap_s8(struct iris_transfer
*map
)
580 struct pipe_transfer
*xfer
= &map
->base
;
581 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
582 struct isl_surf
*surf
= &res
->surf
;
583 const bool has_swizzling
= false;
585 if (xfer
->usage
& PIPE_TRANSFER_WRITE
) {
586 uint8_t *untiled_s8_map
= map
->ptr
;
587 uint8_t *tiled_s8_map
=
588 iris_bo_map(map
->dbg
, res
->bo
, xfer
->usage
| MAP_RAW
);
590 struct pipe_box box
= xfer
->box
;
592 for (int s
= 0; s
< box
.depth
; s
++) {
593 unsigned x0_el
, y0_el
;
594 get_image_offset_el(surf
, xfer
->level
, box
.z
, &x0_el
, &y0_el
);
596 for (uint32_t y
= 0; y
< box
.height
; y
++) {
597 for (uint32_t x
= 0; x
< box
.width
; x
++) {
598 ptrdiff_t offset
= s8_offset(surf
->row_pitch_B
,
602 tiled_s8_map
[offset
] =
603 untiled_s8_map
[s
* xfer
->layer_stride
+ y
* xfer
->stride
+ x
];
615 iris_map_s8(struct iris_transfer
*map
)
617 struct pipe_transfer
*xfer
= &map
->base
;
618 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
619 struct isl_surf
*surf
= &res
->surf
;
621 xfer
->stride
= surf
->row_pitch_B
;
622 xfer
->layer_stride
= xfer
->stride
* xfer
->box
.height
;
624 /* The tiling and detiling functions require that the linear buffer has
625 * a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we
626 * over-allocate the linear buffer to get the proper alignment.
628 map
->buffer
= map
->ptr
= malloc(xfer
->layer_stride
* xfer
->box
.depth
);
631 const bool has_swizzling
= false;
633 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
634 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
635 * invalidate is set, since we'll be writing the whole rectangle from our
636 * temporary buffer back out.
638 if (!(xfer
->usage
& PIPE_TRANSFER_DISCARD_RANGE
)) {
639 uint8_t *untiled_s8_map
= map
->ptr
;
640 uint8_t *tiled_s8_map
=
641 iris_bo_map(map
->dbg
, res
->bo
, xfer
->usage
| MAP_RAW
);
643 struct pipe_box box
= xfer
->box
;
645 for (int s
= 0; s
< box
.depth
; s
++) {
646 unsigned x0_el
, y0_el
;
647 get_image_offset_el(surf
, xfer
->level
, box
.z
, &x0_el
, &y0_el
);
649 for (uint32_t y
= 0; y
< box
.height
; y
++) {
650 for (uint32_t x
= 0; x
< box
.width
; x
++) {
651 ptrdiff_t offset
= s8_offset(surf
->row_pitch_B
,
655 untiled_s8_map
[s
* xfer
->layer_stride
+ y
* xfer
->stride
+ x
] =
656 tiled_s8_map
[offset
];
664 map
->unmap
= iris_unmap_s8
;
667 /* Compute extent parameters for use with tiled_memcpy functions.
668 * xs are in units of bytes and ys are in units of strides.
671 tile_extents(struct isl_surf
*surf
,
672 const struct pipe_box
*box
,
674 unsigned *x1_B
, unsigned *x2_B
,
675 unsigned *y1_el
, unsigned *y2_el
)
677 const struct isl_format_layout
*fmtl
= isl_format_get_layout(surf
->format
);
678 const unsigned cpp
= fmtl
->bpb
/ 8;
680 assert(box
->x
% fmtl
->bw
== 0);
681 assert(box
->y
% fmtl
->bh
== 0);
683 unsigned x0_el
, y0_el
;
684 get_image_offset_el(surf
, level
, box
->z
, &x0_el
, &y0_el
);
686 *x1_B
= (box
->x
/ fmtl
->bw
+ x0_el
) * cpp
;
687 *y1_el
= box
->y
/ fmtl
->bh
+ y0_el
;
688 *x2_B
= (DIV_ROUND_UP(box
->x
+ box
->width
, fmtl
->bw
) + x0_el
) * cpp
;
689 *y2_el
= DIV_ROUND_UP(box
->y
+ box
->height
, fmtl
->bh
) + y0_el
;
693 iris_unmap_tiled_memcpy(struct iris_transfer
*map
)
695 struct pipe_transfer
*xfer
= &map
->base
;
696 struct pipe_box box
= xfer
->box
;
697 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
698 struct isl_surf
*surf
= &res
->surf
;
700 const bool has_swizzling
= false;
702 if (xfer
->usage
& PIPE_TRANSFER_WRITE
) {
703 char *dst
= iris_bo_map(map
->dbg
, res
->bo
, xfer
->usage
| MAP_RAW
);
705 for (int s
= 0; s
< box
.depth
; s
++) {
706 unsigned x1
, x2
, y1
, y2
;
707 tile_extents(surf
, &box
, xfer
->level
, &x1
, &x2
, &y1
, &y2
);
709 void *ptr
= map
->ptr
+ s
* xfer
->layer_stride
;
711 isl_memcpy_linear_to_tiled(x1
, x2
, y1
, y2
, dst
, ptr
,
712 surf
->row_pitch_B
, xfer
->stride
,
713 has_swizzling
, surf
->tiling
, ISL_MEMCPY
);
717 os_free_aligned(map
->buffer
);
718 map
->buffer
= map
->ptr
= NULL
;
722 iris_map_tiled_memcpy(struct iris_transfer
*map
)
724 struct pipe_transfer
*xfer
= &map
->base
;
725 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
726 struct isl_surf
*surf
= &res
->surf
;
728 xfer
->stride
= ALIGN(surf
->row_pitch_B
, 16);
729 xfer
->layer_stride
= xfer
->stride
* xfer
->box
.height
;
731 unsigned x1
, x2
, y1
, y2
;
732 tile_extents(surf
, &xfer
->box
, xfer
->level
, &x1
, &x2
, &y1
, &y2
);
734 /* The tiling and detiling functions require that the linear buffer has
735 * a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we
736 * over-allocate the linear buffer to get the proper alignment.
739 os_malloc_aligned(xfer
->layer_stride
* xfer
->box
.depth
, 16);
741 map
->ptr
= (char *)map
->buffer
+ (x1
& 0xf);
743 const bool has_swizzling
= false;
745 // XXX: PIPE_TRANSFER_READ?
746 if (!(xfer
->usage
& PIPE_TRANSFER_DISCARD_RANGE
)) {
747 char *src
= iris_bo_map(map
->dbg
, res
->bo
, xfer
->usage
| MAP_RAW
);
749 struct pipe_box box
= xfer
->box
;
751 for (int s
= 0; s
< box
.depth
; s
++) {
752 unsigned x1
, x2
, y1
, y2
;
753 tile_extents(surf
, &box
, xfer
->level
, &x1
, &x2
, &y1
, &y2
);
755 /* When transferring cubes, box.depth is counted in cubes, but
756 * box.z is counted in faces. We want to transfer only the
757 * specified face, but for all array elements. So, use 's'
758 * (the zero-based slice count) rather than box.z.
760 void *ptr
= map
->ptr
+ s
* xfer
->layer_stride
;
762 isl_memcpy_tiled_to_linear(x1
, x2
, y1
, y2
, ptr
, src
, xfer
->stride
,
763 surf
->row_pitch_B
, has_swizzling
,
764 surf
->tiling
, ISL_MEMCPY
);
769 map
->unmap
= iris_unmap_tiled_memcpy
;
773 iris_map_direct(struct iris_transfer
*map
)
775 struct pipe_transfer
*xfer
= &map
->base
;
776 struct pipe_box
*box
= &xfer
->box
;
777 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
778 struct isl_surf
*surf
= &res
->surf
;
779 const struct isl_format_layout
*fmtl
= isl_format_get_layout(surf
->format
);
780 const unsigned cpp
= fmtl
->bpb
/ 8;
781 unsigned x0_el
, y0_el
;
783 void *ptr
= iris_bo_map(map
->dbg
, res
->bo
, xfer
->usage
);
785 if (res
->base
.target
== PIPE_BUFFER
) {
787 xfer
->layer_stride
= 0;
789 map
->ptr
= ptr
+ box
->x
;
791 get_image_offset_el(surf
, xfer
->level
, box
->z
, &x0_el
, &y0_el
);
793 xfer
->stride
= isl_surf_get_row_pitch_B(surf
);
794 xfer
->layer_stride
= isl_surf_get_array_pitch(surf
);
796 map
->ptr
= ptr
+ (y0_el
+ box
->y
) * xfer
->stride
+ (x0_el
+ box
->x
) * cpp
;
801 iris_transfer_map(struct pipe_context
*ctx
,
802 struct pipe_resource
*resource
,
804 enum pipe_transfer_usage usage
,
805 const struct pipe_box
*box
,
806 struct pipe_transfer
**ptransfer
)
808 struct iris_context
*ice
= (struct iris_context
*)ctx
;
809 struct iris_resource
*res
= (struct iris_resource
*)resource
;
810 struct isl_surf
*surf
= &res
->surf
;
812 if (surf
->tiling
!= ISL_TILING_LINEAR
&&
813 (usage
& PIPE_TRANSFER_MAP_DIRECTLY
))
816 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) &&
817 iris_batch_references(&ice
->render_batch
, res
->bo
)) {
818 iris_batch_flush(&ice
->render_batch
);
821 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) &&
822 iris_batch_references(&ice
->compute_batch
, res
->bo
)) {
823 iris_batch_flush(&ice
->compute_batch
);
826 if ((usage
& PIPE_TRANSFER_DONTBLOCK
) && iris_bo_busy(res
->bo
))
829 struct iris_transfer
*map
= slab_alloc(&ice
->transfer_pool
);
830 struct pipe_transfer
*xfer
= &map
->base
;
832 // PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
833 // PIPE_TRANSFER_DISCARD_RANGE
838 memset(map
, 0, sizeof(*map
));
839 map
->dbg
= &ice
->dbg
;
841 pipe_resource_reference(&xfer
->resource
, resource
);
847 xfer
->usage
&= (PIPE_TRANSFER_READ
|
848 PIPE_TRANSFER_WRITE
|
849 PIPE_TRANSFER_UNSYNCHRONIZED
|
850 PIPE_TRANSFER_PERSISTENT
|
851 PIPE_TRANSFER_COHERENT
|
852 PIPE_TRANSFER_DISCARD_RANGE
);
854 if (surf
->tiling
== ISL_TILING_W
) {
855 // XXX: just teach iris_map_tiled_memcpy about W tiling...
857 } else if (surf
->tiling
!= ISL_TILING_LINEAR
) {
858 iris_map_tiled_memcpy(map
);
860 iris_map_direct(map
);
867 iris_transfer_unmap(struct pipe_context
*ctx
, struct pipe_transfer
*xfer
)
869 struct iris_context
*ice
= (struct iris_context
*)ctx
;
870 struct iris_transfer
*map
= (void *) xfer
;
871 struct iris_resource
*res
= (struct iris_resource
*) xfer
->resource
;
872 struct isl_surf
*surf
= &res
->surf
;
877 /* XXX: big ol' hack! need to re-emit UBOs. want bind_history? */
878 if (surf
->tiling
== ISL_TILING_LINEAR
) {
879 ice
->state
.dirty
|= IRIS_DIRTY_CONSTANTS_VS
| IRIS_DIRTY_BINDINGS_VS
880 | IRIS_DIRTY_CONSTANTS_TCS
| IRIS_DIRTY_BINDINGS_TCS
881 | IRIS_DIRTY_CONSTANTS_TES
| IRIS_DIRTY_BINDINGS_TES
882 | IRIS_DIRTY_CONSTANTS_GS
| IRIS_DIRTY_BINDINGS_GS
883 | IRIS_DIRTY_CONSTANTS_FS
| IRIS_DIRTY_BINDINGS_FS
;
886 pipe_resource_reference(&xfer
->resource
, NULL
);
887 slab_free(&ice
->transfer_pool
, map
);
891 iris_flush_resource(struct pipe_context
*ctx
, struct pipe_resource
*resource
)
895 static enum pipe_format
896 iris_resource_get_internal_format(struct pipe_resource
*p_res
)
898 struct iris_resource
*res
= (void *) p_res
;
899 return res
->internal_format
;
902 static const struct u_transfer_vtbl transfer_vtbl
= {
903 .resource_create
= iris_resource_create
,
904 .resource_destroy
= iris_resource_destroy
,
905 .transfer_map
= iris_transfer_map
,
906 .transfer_unmap
= iris_transfer_unmap
,
907 .transfer_flush_region
= u_default_transfer_flush_region
,
908 .get_internal_format
= iris_resource_get_internal_format
,
909 .set_stencil
= iris_resource_set_separate_stencil
,
910 .get_stencil
= iris_resource_get_separate_stencil
,
914 iris_init_screen_resource_functions(struct pipe_screen
*pscreen
)
916 pscreen
->resource_create_with_modifiers
=
917 iris_resource_create_with_modifiers
;
918 pscreen
->resource_create
= u_transfer_helper_resource_create
;
919 pscreen
->resource_from_user_memory
= iris_resource_from_user_memory
;
920 pscreen
->resource_from_handle
= iris_resource_from_handle
;
921 pscreen
->resource_get_handle
= iris_resource_get_handle
;
922 pscreen
->resource_destroy
= u_transfer_helper_resource_destroy
;
923 pscreen
->transfer_helper
=
924 u_transfer_helper_create(&transfer_vtbl
, true, true, false, true);
928 iris_init_resource_functions(struct pipe_context
*ctx
)
930 ctx
->flush_resource
= iris_flush_resource
;
931 ctx
->transfer_map
= u_transfer_helper_transfer_map
;
932 ctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
933 ctx
->transfer_unmap
= u_transfer_helper_transfer_unmap
;
934 ctx
->buffer_subdata
= u_default_buffer_subdata
;
935 ctx
->texture_subdata
= u_default_texture_subdata
;