This example demonstrates how a kernel may use texture memory through the texture object API. Using texture memory may be beneficial as the texture cache is optimized for 2D spatial locality and exposes features such as hardware filtering. In the example, a texture is created using a device array and is sampled in a kernel to create a histogram of its values.
- Check whether texture functions are supported on the device.
- Initialize the texture data on host side.
- Specify the channel description of the texture and allocate a device array based on the texture dimensions and channel descriptor.
- Copy the texture data from host to device.
- Specify the texture resource and its parameters, and create the texture object.
- Allocate a device-side histogram.
- Launch the histogram kernel, which creates a histogram of the texture on the device.
- Copy the histogram to host memory and print the results.
- Destroy the texture object and release resources.
- The memory for the texture may be a device array
hipArray_t
, which is allocated withhipMallocArray
. The allocation call requires a channel descriptorhipChannelFormatDesc
and the dimensions of the texture. The channel descriptor can be created usinghipCreateChannelDesc
. Host data can be transferred to the device array usinghipMemcpy2DToArray
. - The texture object
hipTextureObject_t
is created withhipCreateTextureObject
, which requires a resource descriptorhipResourceDesc
and a texture descriptorhipTextureDesc
. The resource descriptor describes the resource used to create the texture, in this example a device arrayhipResourceTypeArray
. The texture descriptor describes the properties of the texture, such as its addressing mode and whether it uses normalized coordinates. - The created texture object can be sampled in a kernel using
tex2D
. - The texture object is cleaned up by calling
hipDestroyTextureObject
and the device array is cleaned up by callinghipFreeArray
.
__global__
atomicAdd
blockDim
blockIdx
tex2D
threadIdx
hipArray_t
hipAddressModeWrap
hipChannelFormatDesc
hipChannelFormatKindUnsigned
hipCreateChannelDesc
hipCreateTextureObject
hipDestroyTextureObject
hipDeviceAttributeImageSupport
hipDeviceGetAttribute
hipFilterModePoint
hipFree
hipFreeArray
hipGetLastError
hipMalloc
hipMallocArray
hipMemcpy
hipMemcpy2DToArray
hipMemcpyHostToDevice
hipMemset
hipReadModeElementType
hipResourceDesc
hipResourceTypeArray
hipStreamDefault
hipTextureDesc
hipTextureObject_t