Files
rocm-systems/api/hip/hip_texture.cpp
T
foreman c49cb353ca P4 to Git Change 1747723 by cpaquot@cpaquot-ocl-lc-lnx on 2019/02/22 19:30:33
SWDEV-145570 - [HIP] Texture object implementation
	hipTextureObject2D test passes on PAL backend.
	Still needs to have sampler handled properly on ROCm backend.

Affected files ...

... //depot/stg/opencl/drivers/opencl/api/hip/hip_internal.hpp#22 edit
... //depot/stg/opencl/drivers/opencl/api/hip/hip_texture.cpp#10 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/device.hpp#333 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/paldevice.hpp#36 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palmemory.hpp#10 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocmemory.hpp#14 edit
2019-02-22 19:43:36 -05:00

548 строки
18 KiB
C++

/*
Copyright (c) 2015 - present Advanced Micro Devices, Inc. All rights reserved.
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.
*/
#include <hip/hip_runtime.h>
#include <hip/hcc_detail/texture_types.h>
#include "hip_internal.hpp"
#include "platform/sampler.hpp"
namespace hip {
struct TextureObject {
uint32_t imageSRD[HIP_IMAGE_OBJECT_SIZE_DWORD];
uint32_t samplerSRD[HIP_SAMPLER_OBJECT_SIZE_DWORD];
amd::Image* image;
amd::Sampler* sampler;
};
};
void getDrvChannelOrderAndType(const enum hipArray_Format Format, unsigned int NumChannels,
cl_channel_order* channelOrder,
cl_channel_type* channelType) {
switch (Format) {
case HIP_AD_FORMAT_UNSIGNED_INT8:
*channelType = CL_UNSIGNED_INT8;
break;
case HIP_AD_FORMAT_UNSIGNED_INT16:
*channelType = CL_UNSIGNED_INT16;
break;
case HIP_AD_FORMAT_UNSIGNED_INT32:
*channelType = CL_UNSIGNED_INT32;
break;
case HIP_AD_FORMAT_SIGNED_INT8:
*channelType = CL_SIGNED_INT8;
break;
case HIP_AD_FORMAT_SIGNED_INT16:
*channelType = CL_SIGNED_INT16;
break;
case HIP_AD_FORMAT_SIGNED_INT32:
*channelType = CL_SIGNED_INT32;
break;
case HIP_AD_FORMAT_HALF:
*channelType = CL_HALF_FLOAT;
break;
case HIP_AD_FORMAT_FLOAT:
*channelType = CL_FLOAT;
break;
default:
break;
}
if (NumChannels == 4) {
*channelOrder = CL_RGBA;
} else if (NumChannels == 2) {
*channelOrder = CL_RG;
} else if (NumChannels == 1) {
*channelOrder = CL_R;
}
}
void getChannelOrderAndType(const hipChannelFormatDesc& desc, enum hipTextureReadMode readMode,
cl_channel_order* channelOrder, cl_channel_type* channelType) {
if (desc.x != 0 && desc.y != 0 && desc.z != 0 && desc.w != 0) {
*channelOrder = CL_RGBA;
} else if (desc.x != 0 && desc.y != 0 && desc.z != 0 && desc.w == 0) {
*channelOrder = CL_RGB;
} else if (desc.x != 0 && desc.y != 0 && desc.z == 0 && desc.w == 0) {
*channelOrder = CL_RG;
} else if (desc.x != 0 && desc.y == 0 && desc.z == 0 && desc.w == 0) {
*channelOrder = CL_R;
} else {
}
switch (desc.f) {
case hipChannelFormatKindUnsigned:
switch (desc.x) {
case 32:
*channelType = CL_UNSIGNED_INT32;
break;
case 16:
*channelType = readMode == hipReadModeNormalizedFloat
? CL_UNORM_INT16
: CL_UNSIGNED_INT16;
break;
case 8:
*channelType = readMode == hipReadModeNormalizedFloat
? CL_UNORM_INT8
: CL_UNSIGNED_INT8;
break;
default:
*channelType = CL_UNSIGNED_INT32;
}
break;
case hipChannelFormatKindSigned:
switch (desc.x) {
case 32:
*channelType = CL_SIGNED_INT32;
break;
case 16:
*channelType = readMode == hipReadModeNormalizedFloat
? CL_SNORM_INT16
: CL_SIGNED_INT16;
break;
case 8:
*channelType = readMode == hipReadModeNormalizedFloat
? CL_SNORM_INT8
: CL_SIGNED_INT8;
break;
default:
*channelType = CL_SIGNED_INT32;
}
break;
case hipChannelFormatKindFloat:
switch (desc.x) {
case 32:
*channelType = CL_FLOAT;
break;
case 16:
*channelType = CL_HALF_FLOAT;
break;
case 8:
break;
default:
*channelType = CL_FLOAT;
}
break;
case hipChannelFormatKindNone:
default:
break;
}
}
amd::Sampler* fillSamplerDescriptor(enum hipTextureAddressMode addressMode,
enum hipTextureFilterMode filterMode, int normalizedCoords) {
#ifndef CL_FILTER_NONE
#define CL_FILTER_NONE 0x1142
#endif
uint32_t filter_mode = CL_FILTER_NONE;
switch (filterMode) {
case hipFilterModePoint:
filter_mode = CL_FILTER_NEAREST;
break;
case hipFilterModeLinear:
filter_mode = CL_FILTER_LINEAR;
break;
}
uint32_t address_mode = CL_ADDRESS_NONE;
switch (addressMode) {
case hipAddressModeWrap:
address_mode = CL_ADDRESS_REPEAT;
break;
case hipAddressModeClamp:
address_mode = CL_ADDRESS_CLAMP;
break;
case hipAddressModeMirror:
address_mode = CL_ADDRESS_MIRRORED_REPEAT;
break;
case hipAddressModeBorder:
address_mode = CL_ADDRESS_CLAMP_TO_EDGE;
break;
}
amd::Sampler* sampler = new amd::Sampler(*hip::getCurrentContext(),
normalizedCoords == CL_TRUE,
address_mode, filter_mode, CL_FILTER_NONE, 0.f, CL_MAXFLOAT);
if (sampler == nullptr) {
return nullptr;
}
if (!sampler->create()) {
delete sampler;
return nullptr;
}
return sampler;
}
hip::TextureObject* ihipCreateTextureObject(amd::Image& image, amd::Sampler& sampler) {
hip::TextureObject* texture;
ihipMalloc(reinterpret_cast<void**>(&texture), sizeof(hip::TextureObject), CL_MEM_SVM_FINE_GRAIN_BUFFER);
if (texture == nullptr) {
return nullptr;
}
device::Memory* imageMem = image.getDeviceMemory(*hip::getCurrentContext()->devices()[0]);
memcpy(texture->imageSRD, imageMem->cpuSrd(), sizeof(uint32_t)*HIP_IMAGE_OBJECT_SIZE_DWORD);
texture->image = &image;
device::Sampler* devSampler = sampler.getDeviceSampler(*hip::getCurrentContext()->devices()[0]);
memcpy(texture->samplerSRD, devSampler->hwState(), sizeof(uint32_t)*HIP_SAMPLER_OBJECT_SIZE_DWORD);
texture->sampler = &sampler;
return texture;
}
hipError_t hipCreateTextureObject(hipTextureObject_t* pTexObject, const hipResourceDesc* pResDesc,
const hipTextureDesc* pTexDesc,
const hipResourceViewDesc* pResViewDesc) {
HIP_INIT_API(pTexObject, pResDesc, pTexDesc, pResViewDesc);
amd::Device* device = hip::getCurrentContext()->devices()[0];
if (!device->info().imageSupport_) {
HIP_RETURN(hipErrorInvalidValue);
}
amd::Image* image = nullptr;
cl_image_format image_format;
getChannelOrderAndType(pResDesc->res.pitch2D.desc, pTexDesc->readMode,
&image_format.image_channel_order, &image_format.image_channel_data_type);
const amd::Image::Format imageFormat(image_format);
amd::Memory* memory = nullptr;
size_t offset = 0;
switch (pResDesc->resType) {
case hipResourceTypeArray:
{
memory = getMemoryObject(pResDesc->res.array.array->data, offset);
getChannelOrderAndType(pResDesc->res.array.array->desc, pTexDesc->readMode,
&image_format.image_channel_order, &image_format.image_channel_data_type);
const amd::Image::Format imageFormat(image_format);
switch (pResDesc->res.array.array->type) {
case hipArrayLayered:
case hipArrayCubemap:
assert(0);
break;
case hipArraySurfaceLoadStore:
case hipArrayTextureGather:
case hipArrayDefault:
default:
image = new (*hip::getCurrentContext()) amd::Image(*memory->asBuffer(),
CL_MEM_OBJECT_IMAGE2D, memory->getMemFlags(), imageFormat,
pResDesc->res.array.array->width, pResDesc->res.array.array->height, 1, 0, 0);
break;
}
}
break;
case hipResourceTypeMipmappedArray:
assert(0);
break;
case hipResourceTypeLinear:
{
assert(pResViewDesc == nullptr);
memory = getMemoryObject(pResDesc->res.linear.devPtr, offset);
getChannelOrderAndType(pResDesc->res.linear.desc, pTexDesc->readMode,
&image_format.image_channel_order, &image_format.image_channel_data_type);
const amd::Image::Format imageFormat(image_format);
image = new (*hip::getCurrentContext()) amd::Image(*memory->asBuffer(),
CL_MEM_OBJECT_IMAGE2D, memory->getMemFlags(), imageFormat,
pResDesc->res.linear.sizeInBytes / imageFormat.getElementSize(), 1, 1,
pResDesc->res.linear.sizeInBytes, 0);
}
break;
case hipResourceTypePitch2D:
assert(pResViewDesc == nullptr);
memory = getMemoryObject(pResDesc->res.pitch2D.devPtr, offset);
image = new (*hip::getCurrentContext()) amd::Image(*memory->asBuffer(),
CL_MEM_OBJECT_IMAGE2D, memory->getMemFlags(), imageFormat,
pResDesc->res.pitch2D.width, pResDesc->res.pitch2D.height, 1,
pResDesc->res.pitch2D.pitchInBytes, 0);
break;
default: HIP_RETURN(hipErrorInvalidValue);
}
if (!image->create()) {
delete image;
HIP_RETURN(hipErrorUnknown);
}
amd::Sampler* sampler = fillSamplerDescriptor(pTexDesc->addressMode[0], pTexDesc->filterMode, pTexDesc->normalizedCoords);
*pTexObject = reinterpret_cast<hipTextureObject_t>(ihipCreateTextureObject(*image, *sampler));
HIP_RETURN(hipSuccess);
}
void ihipDestroyTextureObject(hip::TextureObject* texture) {
texture->image->release();
texture->sampler->release();
hipFree(texture);
}
hipError_t hipDestroyTextureObject(hipTextureObject_t textureObject) {
HIP_INIT_API(textureObject);
hip::TextureObject* texture = reinterpret_cast<hip::TextureObject*>(textureObject);
ihipDestroyTextureObject(texture);
HIP_RETURN(hipSuccess);
}
hipError_t hipGetTextureObjectResourceDesc(hipResourceDesc* pResDesc,
hipTextureObject_t textureObject) {
HIP_INIT_API(pResDesc, textureObject);
assert(0 && "Unimplemented");
HIP_RETURN(hipErrorUnknown);
}
hipError_t hipGetTextureObjectResourceViewDesc(hipResourceViewDesc* pResViewDesc,
hipTextureObject_t textureObject) {
HIP_INIT_API(pResViewDesc, textureObject);
assert(0 && "Unimplemented");
HIP_RETURN(hipErrorUnknown);
}
hipError_t hipGetTextureObjectTextureDesc(hipTextureDesc* pTexDesc,
hipTextureObject_t textureObject) {
HIP_INIT_API(pTexDesc, textureObject);
assert(0 && "Unimplemented");
HIP_RETURN(hipErrorUnknown);
}
hipError_t ihipBindTexture(cl_mem_object_type type,
size_t* offset, textureReference* tex, const void* devPtr,
const hipChannelFormatDesc* desc, size_t width, size_t height,
size_t pitch) {
if (tex == nullptr) {
return hipErrorInvalidImage;
}
if (hip::getCurrentContext()) {
cl_image_format image_format;
if (nullptr == desc) {
getDrvChannelOrderAndType(tex->format, tex->numChannels,
&image_format.image_channel_order, &image_format.image_channel_data_type);
} else {
getChannelOrderAndType(*desc, hipReadModeElementType,
&image_format.image_channel_order, &image_format.image_channel_data_type);
}
const amd::Image::Format imageFormat(image_format);
amd::Memory* memory = getMemoryObject(devPtr, *offset);
amd::Image* image = new (*hip::getCurrentContext()) amd::Image(*memory->asBuffer(),
type, memory->getMemFlags(), imageFormat, width, height, 1, pitch, 0);
if (!image->create()) {
delete image;
return hipErrorUnknown;
}
*offset = 0;
if (tex->textureObject) {
ihipDestroyTextureObject(reinterpret_cast<hip::TextureObject*>(tex->textureObject));
}
amd::Sampler* sampler = fillSamplerDescriptor(tex->addressMode[0], tex->filterMode, tex->normalized);
tex->textureObject = reinterpret_cast<hipTextureObject_t>(ihipCreateTextureObject(*image, *sampler));
return hipSuccess;
}
return hipErrorUnknown;
}
hipError_t hipBindTexture(size_t* offset, textureReference* tex, const void* devPtr,
const hipChannelFormatDesc* desc, size_t size) {
HIP_INIT_API(offset, tex, devPtr, desc, size);
if (desc == nullptr) {
HIP_RETURN(hipErrorInvalidValue);
}
cl_image_format image_format;
getChannelOrderAndType(*desc, hipReadModeElementType,
&image_format.image_channel_order, &image_format.image_channel_data_type);
const amd::Image::Format imageFormat(image_format);
HIP_RETURN(ihipBindTexture(CL_MEM_OBJECT_IMAGE1D, offset, tex, devPtr, desc, size / imageFormat.getElementSize(), 1, size));
}
hipError_t hipBindTexture2D(size_t* offset, textureReference* tex, const void* devPtr,
const hipChannelFormatDesc* desc, size_t width, size_t height,
size_t pitch) {
HIP_INIT_API(offset, tex, devPtr, desc, width, height, pitch);
HIP_RETURN(ihipBindTexture(CL_MEM_OBJECT_IMAGE2D, offset, tex, devPtr, desc, width, height, pitch));
}
hipError_t hipBindTextureToArray(textureReference* tex, hipArray_const_t array,
const hipChannelFormatDesc* desc) {
HIP_INIT_API(tex, array, desc);
assert(0 && "Unimplemented");
HIP_RETURN(hipErrorUnknown);
}
hipError_t ihipBindTextureToArrayImpl(int dim, enum hipTextureReadMode readMode,
hipArray_const_t array,
const struct hipChannelFormatDesc& desc,
textureReference* tex) {
assert(0 && "Unimplemented");
return hipErrorUnknown;
}
hipError_t hipBindTextureToMipmappedArray(textureReference* tex,
hipMipmappedArray_const_t mipmappedArray,
const hipChannelFormatDesc* desc) {
HIP_INIT_API(tex, mipmappedArray, desc);
assert(0 && "Unimplemented");
HIP_RETURN(hipErrorUnknown);
}
hipError_t hipUnbindTexture(const textureReference* tex) {
HIP_INIT_API(tex);
as_amd(reinterpret_cast<cl_mem>(tex->textureObject))->release();
HIP_RETURN(hipSuccess);
}
hipError_t hipGetChannelDesc(hipChannelFormatDesc* desc, hipArray_const_t array) {
HIP_INIT_API(desc, array);
assert(0 && "Unimplemented");
HIP_RETURN(hipErrorUnknown);
}
hipError_t hipGetTextureAlignmentOffset(size_t* offset, const textureReference* tex) {
HIP_INIT_API(offset, tex);
assert(0 && "Unimplemented");
HIP_RETURN(hipErrorUnknown);
}
hipError_t hipGetTextureReference(const textureReference** tex, const void* symbol) {
HIP_INIT_API(tex, symbol);
assert(0 && "Unimplemented");
HIP_RETURN(hipErrorUnknown);
}
hipError_t hipTexRefSetFormat(textureReference* tex, hipArray_Format fmt, int NumPackedComponents) {
HIP_INIT_API(tex, fmt, NumPackedComponents);
if (tex == nullptr) {
HIP_RETURN(hipErrorInvalidImage);
}
tex->format = fmt;
tex->numChannels = NumPackedComponents;
HIP_RETURN(hipSuccess);
}
hipError_t hipTexRefSetFlags(textureReference* tex, unsigned int flags) {
HIP_INIT_API(tex, flags);
if (tex == nullptr) {
HIP_RETURN(hipErrorInvalidImage);
}
tex->normalized = flags;
HIP_RETURN(hipSuccess);
}
hipError_t hipTexRefSetFilterMode(textureReference* tex, hipTextureFilterMode fm) {
HIP_INIT_API(tex, fm);
if (tex == nullptr) {
HIP_RETURN(hipErrorInvalidImage);
}
tex->filterMode = fm;
HIP_RETURN(hipSuccess);
}
hipError_t hipTexRefSetAddressMode(textureReference* tex, int dim, hipTextureAddressMode am) {
HIP_INIT_API(tex, dim, am);
if (tex == nullptr) {
HIP_RETURN(hipErrorInvalidImage);
}
tex->addressMode[dim] = am;
HIP_RETURN(hipSuccess);
}
hipError_t hipTexRefSetArray(textureReference* tex, hipArray_const_t array, unsigned int flags) {
HIP_INIT_API(tex, array, flags);
assert(0 && "Unimplemented");
HIP_RETURN(hipErrorUnknown);
}
hipError_t hipTexRefSetAddress(size_t* offset, textureReference* tex, hipDeviceptr_t devPtr,
size_t size) {
HIP_INIT_API(offset, tex, devPtr, size);
if (tex == nullptr) {
HIP_RETURN(hipErrorInvalidImage);
}
cl_image_format image_format;
getDrvChannelOrderAndType(tex->format, tex->numChannels,
&image_format.image_channel_order, &image_format.image_channel_data_type);
const amd::Image::Format imageFormat(image_format);
HIP_RETURN(ihipBindTexture(CL_MEM_OBJECT_IMAGE1D, offset, tex, devPtr, nullptr, size / imageFormat.getElementSize(), 1, size));
}
hipError_t hipTexRefSetAddress2D(textureReference* tex, const HIP_ARRAY_DESCRIPTOR* desc,
hipDeviceptr_t devPtr, size_t pitch) {
HIP_INIT_API(tex, desc, devPtr, pitch);
if (desc == nullptr) {
HIP_RETURN(hipErrorInvalidValue);
}
size_t offset;
HIP_RETURN(ihipBindTexture(CL_MEM_OBJECT_IMAGE2D, &offset, tex, devPtr, nullptr, desc->width, desc->height, pitch));
}