Skip to content

Commit 8e7f785

Browse files
committed
[HIP] Implement urMemImageGetInfo
1 parent 2417fcf commit 8e7f785

File tree

1 file changed

+85
-5
lines changed

1 file changed

+85
-5
lines changed

source/adapters/hip/memory.cpp

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,91 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreate(
481481
return Result;
482482
}
483483

484-
/// \TODO Not implemented
485-
UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t,
486-
ur_image_info_t, size_t,
487-
void *, size_t *) {
488-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
484+
UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t hMemory,
485+
ur_image_info_t propName,
486+
size_t propSize,
487+
void *pPropValue,
488+
size_t *pPropSizeRet) {
489+
UR_ASSERT(hMemory->isImage(), UR_RESULT_ERROR_INVALID_MEM_OBJECT);
490+
ScopedContext Active(hMemory->getContext()->getDevice());
491+
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
492+
493+
try {
494+
495+
HIP_ARRAY3D_DESCRIPTOR ArrayInfo;
496+
UR_CHECK_ERROR(hipArray3DGetDescriptor(
497+
&ArrayInfo, std::get<SurfaceMem>(hMemory->Mem).Array));
498+
499+
const auto hip2urFormat =
500+
[](hipArray_Format HipFormat) -> ur_image_channel_type_t {
501+
switch (HipFormat) {
502+
case HIP_AD_FORMAT_UNSIGNED_INT8:
503+
return UR_IMAGE_CHANNEL_TYPE_UNSIGNED_INT8;
504+
case HIP_AD_FORMAT_UNSIGNED_INT16:
505+
return UR_IMAGE_CHANNEL_TYPE_UNSIGNED_INT16;
506+
case HIP_AD_FORMAT_UNSIGNED_INT32:
507+
return UR_IMAGE_CHANNEL_TYPE_UNSIGNED_INT32;
508+
case HIP_AD_FORMAT_SIGNED_INT8:
509+
return UR_IMAGE_CHANNEL_TYPE_SIGNED_INT8;
510+
case HIP_AD_FORMAT_SIGNED_INT16:
511+
return UR_IMAGE_CHANNEL_TYPE_SIGNED_INT16;
512+
case HIP_AD_FORMAT_SIGNED_INT32:
513+
return UR_IMAGE_CHANNEL_TYPE_SIGNED_INT32;
514+
case HIP_AD_FORMAT_HALF:
515+
return UR_IMAGE_CHANNEL_TYPE_HALF_FLOAT;
516+
case HIP_AD_FORMAT_FLOAT:
517+
return UR_IMAGE_CHANNEL_TYPE_FLOAT;
518+
519+
default:
520+
detail::ur::die("Invalid Hip format specified.");
521+
}
522+
};
523+
524+
const auto hipFormatToElementSize =
525+
[](hipArray_Format HipFormat) -> size_t {
526+
switch (HipFormat) {
527+
case HIP_AD_FORMAT_UNSIGNED_INT8:
528+
case HIP_AD_FORMAT_SIGNED_INT8:
529+
return 1;
530+
case HIP_AD_FORMAT_UNSIGNED_INT16:
531+
case HIP_AD_FORMAT_SIGNED_INT16:
532+
case HIP_AD_FORMAT_HALF:
533+
return 2;
534+
case HIP_AD_FORMAT_UNSIGNED_INT32:
535+
case HIP_AD_FORMAT_SIGNED_INT32:
536+
case HIP_AD_FORMAT_FLOAT:
537+
return 4;
538+
default:
539+
detail::ur::die("Invalid Hip format specified.");
540+
}
541+
};
542+
543+
switch (propName) {
544+
case UR_IMAGE_INFO_FORMAT:
545+
return ReturnValue(ur_image_format_t{UR_IMAGE_CHANNEL_ORDER_RGBA,
546+
hip2urFormat(ArrayInfo.Format)});
547+
case UR_IMAGE_INFO_WIDTH:
548+
return ReturnValue(ArrayInfo.Width);
549+
case UR_IMAGE_INFO_HEIGHT:
550+
return ReturnValue(ArrayInfo.Height);
551+
case UR_IMAGE_INFO_DEPTH:
552+
return ReturnValue(ArrayInfo.Depth);
553+
case UR_IMAGE_INFO_ELEMENT_SIZE:
554+
return ReturnValue(hipFormatToElementSize(ArrayInfo.Format));
555+
case UR_IMAGE_INFO_ROW_PITCH:
556+
case UR_IMAGE_INFO_SLICE_PITCH:
557+
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
558+
559+
default:
560+
return UR_RESULT_ERROR_INVALID_ENUMERATION;
561+
}
562+
563+
} catch (ur_result_t Err) {
564+
return Err;
565+
} catch (...) {
566+
return UR_RESULT_ERROR_UNKNOWN;
567+
}
568+
return UR_RESULT_SUCCESS;
489569
}
490570

491571
UR_APIEXPORT ur_result_t UR_APICALL urMemRetain(ur_mem_handle_t hMem) {

0 commit comments

Comments
 (0)