Dateien
rocm-systems/runtime/hsa-runtime/image/hsa_ext_image.cpp
T

430 Zeilen
16 KiB
C++

////////////////////////////////////////////////////////////////////////////////
//
// The University of Illinois/NCSA
// Open Source License (NCSA)
//
2020-06-04 23:44:34 -05:00
// Copyright (c) 2014-2020, Advanced Micro Devices, Inc. All rights reserved.
//
// Developed by:
//
// AMD Research and AMD HSA Software Development
//
// Advanced Micro Devices, Inc.
//
// www.amd.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal with the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimers.
// - Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimers in
// the documentation and/or other materials provided with the distribution.
// - Neither the names of Advanced Micro Devices, Inc,
// nor the names of its contributors may be used to endorse or promote
// products derived from this Software without specific prior written
// permission.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS WITH THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////
2020-04-30 00:18:36 -05:00
#include "image_runtime.h"
#include "image/inc/hsa_ext_image_impl.h"
2020-08-28 02:04:10 -05:00
#include "core/inc/exceptions.h"
2020-04-30 00:18:36 -05:00
namespace rocr {
2020-08-28 02:04:10 -05:00
namespace AMD {
hsa_status_t handleException();
template <class T> static __forceinline T handleExceptionT() {
handleException();
abort();
return T();
}
} // namespace amd
#define TRY try {
#define CATCH } catch(...) { return AMD::handleException(); }
#define CATCHRET(RETURN_TYPE) } catch(...) { return AMD::handleExceptionT<RETURN_TYPE>(); }
namespace image {
2020-04-30 00:18:36 -05:00
//---------------------------------------------------------------------------//
// Utilty routines
//---------------------------------------------------------------------------//
static void enforceDefaultPitch(hsa_agent_t agent,
const hsa_ext_image_descriptor_t* image_descriptor,
size_t& image_data_row_pitch, size_t& image_data_slice_pitch) {
2020-04-30 00:18:36 -05:00
// Set default pitch
if (image_data_row_pitch == 0) {
auto manager = ImageRuntime::instance()->image_manager(agent);
2020-04-30 00:18:36 -05:00
assert((manager != nullptr) && "Image manager should already exit.");
image_data_row_pitch = image_descriptor->width *
manager->GetImageProperty(agent, image_descriptor->format, image_descriptor->geometry)
.element_size;
}
// Set default slice pitch
if ((image_data_slice_pitch == 0) &&
((image_descriptor->depth != 0) || (image_descriptor->array_size != 0))) {
switch (image_descriptor->geometry) {
case HSA_EXT_IMAGE_GEOMETRY_3D:
case HSA_EXT_IMAGE_GEOMETRY_2DA:
case HSA_EXT_IMAGE_GEOMETRY_2DADEPTH: {
image_data_slice_pitch = image_data_row_pitch * image_descriptor->height;
break;
}
case HSA_EXT_IMAGE_GEOMETRY_1DA: {
image_data_slice_pitch = image_data_row_pitch;
break;
}
default:
fprintf(stderr, "Depth set on single layer image geometry.\n");
//assert(false && "Depth set on single layer image geometry.");
}
}
}
//---------------------------------------------------------------------------//
// APIs that implement Image functionality
2020-04-30 00:18:36 -05:00
//---------------------------------------------------------------------------//
hsa_status_t hsa_amd_image_get_info_max_dim(hsa_agent_t agent, hsa_agent_info_t attribute,
void* value) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if (value == NULL) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return ImageRuntime::instance()->GetImageInfoMaxDimension(agent, attribute, value);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_image_get_capability(hsa_agent_t agent,
hsa_ext_image_geometry_t image_geometry,
const hsa_ext_image_format_t* image_format,
uint32_t* capability_mask) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if ((image_format == NULL) || (capability_mask == NULL) ||
(image_geometry < HSA_EXT_IMAGE_GEOMETRY_1D) ||
(image_geometry > HSA_EXT_IMAGE_GEOMETRY_2DADEPTH)) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return ImageRuntime::instance()->GetImageCapability(agent, *image_format, image_geometry,
*capability_mask);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_image_data_get_info(hsa_agent_t agent,
const hsa_ext_image_descriptor_t* image_descriptor,
hsa_access_permission_t access_permission,
hsa_ext_image_data_info_t* image_data_info) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if ((image_descriptor == NULL) || (image_data_info == NULL) ||
(access_permission < HSA_ACCESS_PERMISSION_RO) ||
(access_permission > HSA_ACCESS_PERMISSION_RW)) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return ImageRuntime::instance()->GetImageSizeAndAlignment(
2020-04-30 00:18:36 -05:00
agent, *image_descriptor, HSA_EXT_IMAGE_DATA_LAYOUT_OPAQUE, 0, 0, *image_data_info);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_image_create(hsa_agent_t agent,
const hsa_ext_image_descriptor_t* image_descriptor,
const void* image_data, hsa_access_permission_t access_permission,
hsa_ext_image_t* image) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if (image_descriptor == NULL || image_data == NULL || image == NULL) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return ImageRuntime::instance()->CreateImageHandle(
agent, *image_descriptor, image_data, access_permission, HSA_EXT_IMAGE_DATA_LAYOUT_OPAQUE, 0,
0, *image);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_image_destroy(hsa_agent_t agent, hsa_ext_image_t image) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
return ImageRuntime::instance()->DestroyImageHandle(image);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_image_copy(hsa_agent_t agent, hsa_ext_image_t src_image,
const hsa_dim3_t* src_offset, hsa_ext_image_t dst_image,
const hsa_dim3_t* dst_offset, const hsa_dim3_t* range) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if (src_image.handle == 0 || dst_image.handle == 0 || src_offset == NULL ||
dst_offset == NULL || range == NULL) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return ImageRuntime::instance()->CopyImage(src_image, dst_image, *src_offset, *dst_offset,
*range);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_image_import(hsa_agent_t agent, const void* src_memory, size_t src_row_pitch,
size_t src_slice_pitch, hsa_ext_image_t dst_image,
const hsa_ext_image_region_t* image_region) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if (src_memory == NULL || dst_image.handle == 0 || image_region == NULL) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return ImageRuntime::instance()->CopyBufferToImage(src_memory, src_row_pitch, src_slice_pitch,
dst_image, *image_region);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_image_export(hsa_agent_t agent, hsa_ext_image_t src_image, void* dst_memory,
size_t dst_row_pitch, size_t dst_slice_pitch,
const hsa_ext_image_region_t* image_region) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if (dst_memory == NULL || src_image.handle == 0 || image_region == NULL) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return ImageRuntime::instance()->CopyImageToBuffer(src_image, dst_memory, dst_row_pitch,
dst_slice_pitch, *image_region);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_image_clear(hsa_agent_t agent, hsa_ext_image_t image, const void* data,
const hsa_ext_image_region_t* image_region) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if (image.handle == 0 || image_region == NULL || data == NULL) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return ImageRuntime::instance()->FillImage(image, data, *image_region);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
};
hsa_status_t hsa_ext_sampler_create(hsa_agent_t agent,
const hsa_ext_sampler_descriptor_t* sampler_descriptor,
hsa_ext_sampler_t* sampler) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
2024-11-15 17:05:04 -05:00
if (sampler_descriptor == NULL || sampler == NULL) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
hsa_ext_sampler_descriptor_v2_t sampler_descriptor_v2 = {
sampler_descriptor->coordinate_mode,
sampler_descriptor->filter_mode,
{sampler_descriptor->address_mode,
sampler_descriptor->address_mode, sampler_descriptor->address_mode}
};
return ImageRuntime::instance()->CreateSamplerHandle(agent, sampler_descriptor_v2, *sampler);
CATCH;
}
hsa_status_t hsa_ext_sampler_create_v2(hsa_agent_t agent,
const hsa_ext_sampler_descriptor_v2_t* sampler_descriptor,
hsa_ext_sampler_t* sampler) {
TRY;
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
2020-04-30 00:18:36 -05:00
if (sampler_descriptor == NULL || sampler == NULL) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return ImageRuntime::instance()->CreateSamplerHandle(agent, *sampler_descriptor, *sampler);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_sampler_destroy(hsa_agent_t agent, hsa_ext_sampler_t sampler) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
return ImageRuntime::instance()->DestroySamplerHandle(sampler);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_image_get_capability_with_layout(hsa_agent_t agent,
hsa_ext_image_geometry_t image_geometry,
const hsa_ext_image_format_t* image_format,
hsa_ext_image_data_layout_t image_data_layout,
uint32_t* capability_mask) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if ((image_format == NULL) || (capability_mask == NULL) ||
(image_geometry < HSA_EXT_IMAGE_GEOMETRY_1D) ||
(image_geometry > HSA_EXT_IMAGE_GEOMETRY_2DADEPTH) ||
(image_data_layout != HSA_EXT_IMAGE_DATA_LAYOUT_LINEAR)) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return ImageRuntime::instance()->GetImageCapability(agent, *image_format, image_geometry,
*capability_mask);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_image_data_get_info_with_layout(
2020-04-30 00:18:36 -05:00
hsa_agent_t agent, const hsa_ext_image_descriptor_t* image_descriptor,
hsa_access_permission_t access_permission, hsa_ext_image_data_layout_t image_data_layout,
size_t image_data_row_pitch, size_t image_data_slice_pitch,
2020-04-30 00:18:36 -05:00
hsa_ext_image_data_info_t* image_data_info) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if ((image_descriptor == NULL) || (image_data_info == NULL) ||
(access_permission < HSA_ACCESS_PERMISSION_RO) ||
(access_permission > HSA_ACCESS_PERMISSION_RW) ||
(image_data_layout != HSA_EXT_IMAGE_DATA_LAYOUT_LINEAR)) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
enforceDefaultPitch(agent, image_descriptor, image_data_row_pitch, image_data_slice_pitch);
return ImageRuntime::instance()->GetImageSizeAndAlignment(
agent, *image_descriptor, image_data_layout, image_data_row_pitch, image_data_slice_pitch,
*image_data_info);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_ext_image_create_with_layout(
hsa_agent_t agent, const hsa_ext_image_descriptor_t* image_descriptor, const void* image_data,
hsa_access_permission_t access_permission, hsa_ext_image_data_layout_t image_data_layout,
size_t image_data_row_pitch, size_t image_data_slice_pitch, hsa_ext_image_t* image) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if (image_descriptor == NULL || image_data == NULL || image == NULL ||
image_data_layout != HSA_EXT_IMAGE_DATA_LAYOUT_LINEAR) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
enforceDefaultPitch(agent, image_descriptor, image_data_row_pitch, image_data_slice_pitch);
return ImageRuntime::instance()->CreateImageHandle(
2020-04-30 00:18:36 -05:00
agent, *image_descriptor, image_data, access_permission, image_data_layout,
image_data_row_pitch, image_data_slice_pitch, *image);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
hsa_status_t hsa_amd_image_create(hsa_agent_t agent,
const hsa_ext_image_descriptor_t* image_descriptor,
const hsa_amd_image_descriptor_t* image_layout,
const void* image_data, hsa_access_permission_t access_permission,
hsa_ext_image_t* image) {
2020-08-28 02:04:10 -05:00
TRY;
2020-04-30 00:18:36 -05:00
if (agent.handle == 0) {
return HSA_STATUS_ERROR_INVALID_AGENT;
}
if (image_descriptor == NULL || image_data == NULL || image == NULL) {
return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return ImageRuntime::instance()->CreateImageHandleWithLayout(
2020-04-30 00:18:36 -05:00
agent, *image_descriptor, image_layout, image_data, access_permission, *image);
2020-08-28 02:04:10 -05:00
CATCH;
2020-04-30 00:18:36 -05:00
}
void LoadImage(core::ImageExtTableInternal* image_api,
decltype(::hsa_amd_image_create)** interface_api) {
image_api->hsa_ext_image_get_capability_fn = hsa_ext_image_get_capability;
image_api->hsa_ext_image_data_get_info_fn = hsa_ext_image_data_get_info;
image_api->hsa_ext_image_create_fn = hsa_ext_image_create;
image_api->hsa_ext_image_import_fn = hsa_ext_image_import;
image_api->hsa_ext_image_export_fn = hsa_ext_image_export;
image_api->hsa_ext_image_copy_fn = hsa_ext_image_copy;
image_api->hsa_ext_image_clear_fn = hsa_ext_image_clear;
image_api->hsa_ext_image_destroy_fn = hsa_ext_image_destroy;
image_api->hsa_ext_sampler_create_fn = hsa_ext_sampler_create;
image_api->hsa_ext_sampler_destroy_fn = hsa_ext_sampler_destroy;
image_api->hsa_ext_image_get_capability_with_layout_fn = hsa_ext_image_get_capability_with_layout;
image_api->hsa_ext_image_data_get_info_with_layout_fn = hsa_ext_image_data_get_info_with_layout;
image_api->hsa_ext_image_create_with_layout_fn = hsa_ext_image_create_with_layout;
image_api->hsa_amd_image_get_info_max_dim_fn = hsa_amd_image_get_info_max_dim;
2024-11-15 17:05:04 -05:00
image_api->hsa_ext_sampler_create_v2_fn = hsa_ext_sampler_create_v2;
*interface_api = hsa_amd_image_create;
2020-04-30 00:18:36 -05:00
}
void ReleaseImageRsrcs() { ImageRuntime::DestroySingleton(); }
} // namespace image
} // namespace rocr