4 #include "image_allocator_gpu.h"
5 #include <thrust/device_ptr.h>
12 template<
typename PixelType,
bool zero_reminder>
14 static int stride(
int pitch_){
15 return pitch_/
sizeof(PixelType);
19 template<
typename PixelType>
21 static int __attribute__((deprecated(
"this function may return wrong result in case pitch_ is not divisible by sizeof(PixelType) (typically the case when using _C3 images)"))) stride(
int pitch_){
22 if(pitch_ %
sizeof(PixelType) != 0){
23 throw std::runtime_error(
"bad! your are using wrong stride, because pitch is not divisible (are you using _C3 images? You shouldn't)");
25 return pitch_/
sizeof(PixelType);
30 template<
typename PixelType,
class Allocator>
53 Allocator::free(
data_);
59 IU_CUDA_SAFE_CALL(cudaDestroyTextureObject(
texture_));
66 ImageGpu(
unsigned int _width,
unsigned int _height) :
90 ImageGpu(PixelType* _data,
unsigned int _width,
unsigned int _height,
91 size_t _pitch,
bool ext_data_pointer =
false) :
115 IU_CUDA_SAFE_CALL(cudaMemcpy2D(&value,
sizeof(PixelType), &
data_[y*
stride()+x],
pitch_,
116 sizeof(PixelType), 1, cudaMemcpyDeviceToHost));
145 return 8*
sizeof(PixelType);
160 PixelType*
data(
int ox = 0,
int oy = 0)
163 return (PixelType*)( (
char*)
data_ +
pitch_*oy + ox*
sizeof(PixelType) );
172 const PixelType*
data(
int ox = 0,
int oy = 0)
const
182 thrust::device_ptr<PixelType>
begin(
void)
184 return thrust::device_ptr<PixelType>(
data());
190 thrust::device_ptr<PixelType>
end(
void)
202 cudaTextureFilterMode filterMode = cudaFilterModeLinear,
203 cudaTextureAddressMode addressMode = cudaAddressModeClamp)
206 IU_CUDA_SAFE_CALL(cudaDestroyTextureObject(
texture_));
208 cudaResourceDesc resDesc;
209 memset(&resDesc, 0,
sizeof(resDesc));
211 cudaTextureDesc texDesc;
212 memset(&texDesc, 0,
sizeof(texDesc));
214 resDesc.resType = cudaResourceTypePitch2D;
215 resDesc.res.pitch2D.devPtr =
data();
216 resDesc.res.pitch2D.pitchInBytes =
pitch();
217 resDesc.res.pitch2D.width =
width();
218 resDesc.res.pitch2D.height =
height();
219 resDesc.res.pitch2D.desc = cudaCreateChannelDesc<PixelType>();
221 texDesc.readMode = readMode;
222 texDesc.normalizedCoords = (addressMode == cudaAddressModeClamp) ?
false :
true;
223 texDesc.addressMode[0] = addressMode;
224 texDesc.addressMode[1] = addressMode;
225 texDesc.filterMode = filterMode;
227 IU_CUDA_SAFE_CALL(cudaCreateTextureObject(&
texture_, &resDesc, &texDesc, NULL));
249 throw IuException(
"Warning: getTexture() on const image requires explicit call to prepareTexture(),"
250 " returned cudaTextureObject will be invalid\n", __FILE__, __FUNCTION__, __LINE__);
iu::Size< 2 > size() const
Definition: image.h:64
const PixelType * data(int ox=0, int oy=0) const
Definition: image_gpu.h:172
__host__ KernelData(const ImageGpu< PixelType, Allocator > &im)
Definition: image_gpu.h:305
Base class for 2D images (pitched memory).
Definition: image.h:30
PixelType pixel_type
Definition: image_gpu.h:38
IUCORE_DLLAPI void copy(const LinearHostMemory_8u_C1 *src, LinearHostMemory_8u_C1 *dst)
cudaTextureObject_t getTexture()
Definition: image_gpu.h:231
thrust::device_ptr< PixelType > end(void)
Definition: image_gpu.h:190
PixelType * data_
Definition: image_gpu.h:319
cudaTextureObject_t texture_
Definition: image_gpu.h:325
__device__ PixelType & operator()(int x, int y)
Definition: image_gpu.h:299
virtual unsigned int bitDepth() const
Definition: image_gpu.h:143
PixelType * data_
Definition: image_gpu.h:286
size_t pitch_
Definition: image_gpu.h:321
void prepareTexture(cudaTextureReadMode readMode=cudaReadModeElementType, cudaTextureFilterMode filterMode=cudaFilterModeLinear, cudaTextureAddressMode addressMode=cudaAddressModeClamp)
Definition: image_gpu.h:201
Exceptions with additional error information.
Definition: coredefs.h:32
ImageGpu(unsigned int _width, unsigned int _height)
Definition: image_gpu.h:66
ImageGpu(PixelType *_data, unsigned int _width, unsigned int _height, size_t _pitch, bool ext_data_pointer=false)
Definition: image_gpu.h:90
Struct pointer KernelData that can be used in CUDA kernels.
Definition: image_gpu.h:283
virtual ~ImageGpu()
Definition: image_gpu.h:48
Definition: image_cpu.h:7
ImageGpu(const iu::Size< 2 > &size)
Definition: image_gpu.h:76
PixelType getPixel(unsigned int x, unsigned int y)
Definition: image_gpu.h:112
virtual bool onDevice() const
Definition: image_gpu.h:149
virtual size_t stride() const
Definition: image_gpu.h:136
int stride_
Definition: image_gpu.h:292
ImageGpu()
Definition: image_gpu.h:41
ndarray_ref< PixelType, 2 > ref() const
bool ext_data_pointer_
Definition: image_gpu.h:323
thrust::device_ptr< PixelType > begin(void)
Definition: image_gpu.h:182
Template specialization for 2-d unsigned int vectors (size vectors).
Definition: vector.h:525
unsigned int width() const
Definition: image.h:72
int width_
Definition: image_gpu.h:288
cudaTextureObject_t getTexture() const
Definition: image_gpu.h:246
virtual size_t pitch() const
Definition: image_gpu.h:127
Definition: image_gpu.h:13
Device 2D image class (pitched memory).
Definition: image_gpu.h:34
int height_
Definition: image_gpu.h:290
PixelType * data(int ox=0, int oy=0)
Definition: image_gpu.h:160
virtual size_t bytes() const
Definition: image_gpu.h:121
unsigned int height() const
Definition: image.h:80