Skip to content

Commit c980982

Browse files
committed
Use the right units for offsets and region sizes
1 parent 6b33f17 commit c980982

File tree

2 files changed

+69
-31
lines changed

2 files changed

+69
-31
lines changed

sycl/source/handler.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,28 @@ fill_copy_args(detail::handler_impl *impl,
258258
auto ZCopyExtentComponent = detail::fill_image_type(SrcImgDesc, UrSrcDesc);
259259
detail::fill_image_type(DestImgDesc, UrDestDesc);
260260

261-
impl->MSrcOffset = {SrcOffset[0], SrcOffset[1], SrcOffset[2]};
262-
impl->MDestOffset = {DestOffset[0], DestOffset[1], DestOffset[2]};
261+
// ur_rect_offset_t and ur_rect_offset_t which represent image offsets and
262+
// copy extents expect that X-axis offset and region width are specified in
263+
// bytes rather then in elements.
264+
auto SrcPixelSize =
265+
SrcImgDesc.num_channels * detail::get_channel_size(SrcImgDesc);
266+
auto DestPixelSize =
267+
DestImgDesc.num_channels * detail::get_channel_size(DestImgDesc);
268+
269+
impl->MSrcOffset = {SrcOffset[0] * SrcPixelSize, SrcOffset[1], SrcOffset[2]};
270+
impl->MDestOffset = {DestOffset[0] * DestPixelSize, DestOffset[1],
271+
DestOffset[2]};
263272
impl->MSrcImageDesc = UrSrcDesc;
264273
impl->MDstImageDesc = UrDestDesc;
265274
impl->MSrcImageFormat = UrSrcFormat;
266275
impl->MDstImageFormat = UrDestFormat;
267276
impl->MImageCopyFlags = ImageCopyFlags;
268277

269278
if (CopyExtent.size() != 0) {
270-
impl->MCopyExtent = {CopyExtent[0], CopyExtent[1], CopyExtent[2]};
279+
impl->MCopyExtent = {CopyExtent[0] * SrcPixelSize, CopyExtent[1],
280+
CopyExtent[2]};
271281
} else {
272-
impl->MCopyExtent = {SrcImgDesc.width, SrcImgDesc.height,
282+
impl->MCopyExtent = {SrcImgDesc.width * SrcPixelSize, SrcImgDesc.height,
273283
ZCopyExtentComponent};
274284
}
275285

unified-runtime/source/adapters/level_zero/image_common.cpp

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -787,13 +787,21 @@ ur_result_t bindlessImagesHandleCopyFlags(
787787
ze_image_region_t DstRegion;
788788
UR_CALL(getImageRegionHelper(zeSrcImageDesc, &pCopyRegion->dstOffset,
789789
&pCopyRegion->copyExtent, DstRegion));
790+
// UR accepts copy regions as byte-pixel-pixel format, i.e. X-axis offset
791+
// and width are specified as bytes, whilst Y/Z-axis offsets, height and
792+
// depth are specified as pixels (or rows and slices). ze_image_region_t,
793+
// however, accepts everything as pixels, so we need to do a conversion
794+
// here.
795+
auto PixelSizeInBytes = getPixelSizeBytes(pSrcImageFormat);
796+
DstRegion.originX /= PixelSizeInBytes;
797+
DstRegion.width /= PixelSizeInBytes;
798+
790799
auto *urDstImg = static_cast<ur_bindless_mem_handle_t *>(pDst);
791800

792-
const char *SrcPtr =
793-
static_cast<const char *>(pSrc) +
794-
pCopyRegion->srcOffset.z * SrcSlicePitch +
795-
pCopyRegion->srcOffset.y * SrcRowPitch +
796-
pCopyRegion->srcOffset.x * getPixelSizeBytes(pSrcImageFormat);
801+
const char *SrcPtr = static_cast<const char *>(pSrc) +
802+
pCopyRegion->srcOffset.z * SrcSlicePitch +
803+
pCopyRegion->srcOffset.y * SrcRowPitch +
804+
pCopyRegion->srcOffset.x;
797805

798806
ZE2UR_CALL(zeCommandListAppendImageCopyFromMemoryExt,
799807
(ZeCommandList, urDstImg->getZeImage(), SrcPtr, &DstRegion,
@@ -830,13 +838,20 @@ ur_result_t bindlessImagesHandleCopyFlags(
830838
ze_image_region_t SrcRegion;
831839
UR_CALL(getImageRegionHelper(zeSrcImageDesc, &pCopyRegion->srcOffset,
832840
&pCopyRegion->copyExtent, SrcRegion));
841+
// UR accepts copy regions as byte-pixel-pixel format, i.e. X-axis offset
842+
// and width are specified as bytes, whilst Y/Z-axis offsets, height and
843+
// depth are specified as pixels (or rows and slices). ze_image_region_t,
844+
// however, accepts everything as pixels, so we need to do a conversion
845+
// here.
846+
auto PixelSizeInBytes = getPixelSizeBytes(pSrcImageFormat);
847+
SrcRegion.originX /= PixelSizeInBytes;
848+
SrcRegion.width /= PixelSizeInBytes;
833849

834850
auto *urSrcImg = reinterpret_cast<const ur_bindless_mem_handle_t *>(pSrc);
835851

836852
char *DstPtr =
837853
static_cast<char *>(pDst) + pCopyRegion->dstOffset.z * DstSlicePitch +
838-
pCopyRegion->dstOffset.y * DstRowPitch +
839-
pCopyRegion->dstOffset.x * getPixelSizeBytes(pDstImageFormat);
854+
pCopyRegion->dstOffset.y * DstRowPitch + pCopyRegion->dstOffset.x;
840855
ZE2UR_CALL(zeCommandListAppendImageCopyToMemoryExt,
841856
(ZeCommandList, DstPtr, urSrcImg->getZeImage(), &SrcRegion,
842857
DstRowPitch, DstSlicePitch, zeSignalEvent, numWaitEvents,
@@ -866,11 +881,30 @@ ur_result_t bindlessImagesHandleCopyFlags(
866881
};
867882
case UR_EXP_IMAGE_COPY_FLAG_DEVICE_TO_DEVICE: {
868883
ze_image_region_t DstRegion;
869-
UR_CALL(getImageRegionHelper(zeSrcImageDesc, &pCopyRegion->dstOffset,
884+
UR_CALL(getImageRegionHelper(zeSrcImageDesc,
885+
&pCopyRegion->dstOffset,
870886
&pCopyRegion->copyExtent, DstRegion));
887+
// UR accepts copy regions as byte-pixel-pixel format, i.e. X-axis offset
888+
// and width are specified as bytes, whilst Y/Z-axis offsets, height and
889+
// depth are specified as pixels (or rows and slices). ze_image_region_t,
890+
// however, accepts everything as pixels, so we need to do a conversion
891+
// here.
892+
auto PixelSizeInBytes = getPixelSizeBytes(pSrcImageFormat);
893+
DstRegion.originX /= PixelSizeInBytes;
894+
DstRegion.width /= PixelSizeInBytes;
895+
871896
ze_image_region_t SrcRegion;
872-
UR_CALL(getImageRegionHelper(zeSrcImageDesc, &pCopyRegion->srcOffset,
897+
UR_CALL(getImageRegionHelper(zeSrcImageDesc,
898+
&pCopyRegion->srcOffset,
873899
&pCopyRegion->copyExtent, SrcRegion));
900+
// UR accepts copy regions as byte-pixel-pixel format, i.e. X-axis offset
901+
// and width are specified as bytes, whilst Y/Z-axis offsets, height and
902+
// depth are specified as pixels (or rows and slices). ze_image_region_t,
903+
// however, accepts everything as pixels, so we need to do a conversion
904+
// here.
905+
PixelSizeInBytes = getPixelSizeBytes(pSrcImageFormat);
906+
SrcRegion.originX /= PixelSizeInBytes;
907+
SrcRegion.width /= PixelSizeInBytes;
874908

875909
auto *urImgSrc = reinterpret_cast<const ur_bindless_mem_handle_t *>(pSrc);
876910
auto *urImgDst = reinterpret_cast<ur_bindless_mem_handle_t *>(pDst);
@@ -884,26 +918,20 @@ ur_result_t bindlessImagesHandleCopyFlags(
884918
};
885919
case UR_EXP_IMAGE_COPY_FLAG_HOST_TO_HOST: {
886920
// Copy between (possibly) pitched USM regions
921+
ze_copy_region_t ZeDstRegion = {(uint32_t)pCopyRegion->dstOffset.x,
922+
(uint32_t)pCopyRegion->dstOffset.y,
923+
(uint32_t)pCopyRegion->dstOffset.z,
924+
(uint32_t)pCopyRegion->copyExtent.width,
925+
(uint32_t)pCopyRegion->copyExtent.height,
926+
(uint32_t)pCopyRegion->copyExtent.depth};
927+
ze_copy_region_t ZeSrcRegion = {(uint32_t)pCopyRegion->dstOffset.x,
928+
(uint32_t)pCopyRegion->srcOffset.y,
929+
(uint32_t)pCopyRegion->srcOffset.z,
930+
(uint32_t)pCopyRegion->copyExtent.width,
931+
(uint32_t)pCopyRegion->copyExtent.height,
932+
(uint32_t)pCopyRegion->copyExtent.depth};
887933
uint32_t DstRowPitch = pDstImageDesc->rowPitch;
888934
uint32_t SrcRowPitch = pSrcImageDesc->rowPitch;
889-
ze_copy_region_t ZeDstRegion = {
890-
(uint32_t)(pCopyRegion->dstOffset.x *
891-
getPixelSizeBytes(pDstImageFormat)),
892-
(uint32_t)pCopyRegion->dstOffset.y,
893-
(uint32_t)pCopyRegion->dstOffset.z,
894-
(uint32_t)(pCopyRegion->copyExtent.width *
895-
getPixelSizeBytes(pDstImageFormat)),
896-
(uint32_t)pCopyRegion->copyExtent.height,
897-
(uint32_t)pCopyRegion->copyExtent.depth};
898-
ze_copy_region_t ZeSrcRegion = {
899-
(uint32_t)(pCopyRegion->dstOffset.x *
900-
getPixelSizeBytes(pSrcImageFormat)),
901-
(uint32_t)pCopyRegion->srcOffset.y,
902-
(uint32_t)pCopyRegion->srcOffset.z,
903-
(uint32_t)(pCopyRegion->copyExtent.width *
904-
getPixelSizeBytes(pSrcImageFormat)),
905-
(uint32_t)pCopyRegion->copyExtent.height,
906-
(uint32_t)pCopyRegion->copyExtent.depth};
907935
uint32_t DstSlicePitch = DstRowPitch * pDstImageDesc->height;
908936
uint32_t SrcSlicePitch = SrcRowPitch * pSrcImageDesc->height;
909937
ZE2UR_CALL(zeCommandListAppendMemoryCopyRegion,

0 commit comments

Comments
 (0)