2022-02-21 10:10:04 -08:00
|
|
|
/* Copyright (c) 2015 - 2022 Advanced Micro Devices, Inc.
|
2020-02-04 08:45:01 -08:00
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in 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:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
AUTHORS 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 IN
|
|
|
|
|
THE SOFTWARE. */
|
2018-04-05 15:02:43 -04:00
|
|
|
|
|
|
|
|
#include <hip/hip_runtime.h>
|
|
|
|
|
|
|
|
|
|
#include "hip_internal.hpp"
|
2021-08-02 18:48:15 +00:00
|
|
|
#include <hip/surface_types.h>
|
2018-04-05 15:02:43 -04:00
|
|
|
|
2022-01-07 15:15:59 -08:00
|
|
|
hipError_t ihipFree(void* ptr);
|
|
|
|
|
|
|
|
|
|
struct __hip_surface {
|
|
|
|
|
uint32_t imageSRD[HIP_IMAGE_OBJECT_SIZE_DWORD];
|
|
|
|
|
amd::Image* image;
|
|
|
|
|
hipResourceDesc resDesc;
|
|
|
|
|
|
|
|
|
|
__hip_surface(amd::Image* image_, const hipResourceDesc& resDesc_)
|
|
|
|
|
: image(image_), resDesc(resDesc_) {
|
|
|
|
|
amd::Context& context = *hip::getCurrentDevice()->asContext();
|
|
|
|
|
amd::Device& device = *context.devices()[0];
|
|
|
|
|
|
|
|
|
|
device::Memory* imageMem = image->getDeviceMemory(device);
|
|
|
|
|
std::memcpy(imageSRD, imageMem->cpuSrd(), sizeof(imageSRD));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
hipError_t ihipCreateSurfaceObject(hipSurfaceObject_t* pSurfObject,
|
|
|
|
|
const hipResourceDesc* pResDesc) {
|
|
|
|
|
amd::Device* device = hip::getCurrentDevice()->devices()[0];
|
|
|
|
|
const device::Info& info = device->info();
|
2023-03-24 15:33:50 -04:00
|
|
|
if (!info.imageSupport_) {
|
|
|
|
|
LogPrintfError("Texture not supported on the device %s", info.name_);
|
|
|
|
|
return hipErrorNotSupported;
|
|
|
|
|
}
|
2022-01-07 15:15:59 -08:00
|
|
|
|
|
|
|
|
// Validate input params
|
|
|
|
|
if (pSurfObject == nullptr || pResDesc == nullptr) {
|
|
|
|
|
return hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the type of resource must be a HIP array
|
|
|
|
|
// hipResourceDesc::res::array::array must be set to a valid HIP array handle.
|
|
|
|
|
if ((pResDesc->resType != hipResourceTypeArray) || (pResDesc->res.array.array == nullptr)) {
|
|
|
|
|
return hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
amd::Image* image = nullptr;
|
|
|
|
|
cl_mem memObj = reinterpret_cast<cl_mem>(pResDesc->res.array.array->data);
|
|
|
|
|
if (!is_valid(memObj)) {
|
|
|
|
|
return hipErrorInvalidValue;
|
|
|
|
|
}
|
|
|
|
|
image = as_amd(memObj)->asImage();
|
|
|
|
|
|
|
|
|
|
void* surfObjectBuffer = nullptr;
|
2022-03-02 11:46:56 -08:00
|
|
|
hipError_t err = ihipMalloc(&surfObjectBuffer, sizeof(__hip_surface),
|
|
|
|
|
CL_MEM_SVM_FINE_GRAIN_BUFFER);
|
|
|
|
|
if (surfObjectBuffer == nullptr || err != hipSuccess) {
|
2022-01-07 15:15:59 -08:00
|
|
|
return hipErrorOutOfMemory;
|
|
|
|
|
}
|
|
|
|
|
*pSurfObject = new (surfObjectBuffer) __hip_surface{image, *pResDesc};
|
|
|
|
|
|
|
|
|
|
return hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 15:02:43 -04:00
|
|
|
hipError_t hipCreateSurfaceObject(hipSurfaceObject_t* pSurfObject,
|
|
|
|
|
const hipResourceDesc* pResDesc) {
|
2020-02-20 18:54:58 -05:00
|
|
|
HIP_INIT_API(hipCreateSurfaceObject, pSurfObject, pResDesc);
|
2018-04-05 15:02:43 -04:00
|
|
|
|
2022-01-07 15:15:59 -08:00
|
|
|
HIP_RETURN(ihipCreateSurfaceObject(pSurfObject, pResDesc));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t ihipDestroySurfaceObject(hipSurfaceObject_t surfaceObject) {
|
|
|
|
|
if (surfaceObject == nullptr) {
|
|
|
|
|
return hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ihipFree(surfaceObject);
|
2018-04-05 15:02:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hipError_t hipDestroySurfaceObject(hipSurfaceObject_t surfaceObject) {
|
2020-02-20 18:54:58 -05:00
|
|
|
HIP_INIT_API(hipDestroySurfaceObject, surfaceObject);
|
2018-04-05 15:02:43 -04:00
|
|
|
|
2022-01-07 15:15:59 -08:00
|
|
|
HIP_RETURN(ihipDestroySurfaceObject(surfaceObject));
|
2018-08-21 18:29:16 -04:00
|
|
|
}
|