QTrk
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
cudaImageList< T > Struct Template Reference

Stores a large number of small images into a single large memory space. Optimizes GPU memory copies. It has no constructor/destructor, so it can be passed to CUDA kernels. The memory is allocated using cudaMallocPitch. It allows binding to a texture. More...

#include <cudaImageList.h>

Public Types

enum  { MaxImageWidth = 8192 }
 

Public Member Functions

CUBOTH int fullwidth ()
 
CUBOTH int fullheight ()
 
CUBOTH int capacity ()
 
CUBOTH int numpixels ()
 
CUBOTH bool isEmpty ()
 
template<int Flags>
void allocateHostImageBuffer (pinned_array< T, Flags > &hostImgBuf)
 
CUBOTH T * get (int i)
 
CUBOTHpixel_oobcheck (int x, int y, int imgIndex, T border=0.0f)
 
CUBOTH T & pixel (int x, int y, int imgIndex)
 
CUBOTH T * pixelAddress (int x, int y, int imgIndex)
 
CUBOTH bool boundaryHit (float2 center, float radius)
 
void free ()
 
void copyImageToHost (int img, T *dst, bool async=false, cudaStream_t s=0)
 
void copyImageToDevice (int img, T *src, bool async=false, cudaStream_t s=0)
 
void copyToHost (T *dst, bool async=false, cudaStream_t s=0)
 
void copyToDevice (T *src, bool async=false, cudaStream_t s=0)
 
void copyToDevice (T *src, int numImages, bool async=false, cudaStream_t s=0)
 
void clear ()
 
CUBOTH int totalNumPixels ()
 
CUBOTH int totalNumBytes ()
 
CUBOTHinterpolate (float x, float y, int idx, bool &outside)
 
void bind (texture< T, cudaTextureType2D, cudaReadModeElementType > &texref)
 
void unbind (texture< T, cudaTextureType2D, cudaReadModeElementType > &texref)
 
CUBOTH void computeImagePos (int &x, int &y, int idx)
 
__device__ T interpolateFromTexture (texture< T, cudaTextureType2D, cudaReadModeElementType > texref, float x, float y, int idx, bool &outside)
 

Static Public Member Functions

static cudaImageList< T > emptyList ()
 
static cudaImageList< T > alloc (int w, int h, int amount)
 
static CUBOTHinterp (T a, T b, float x)
 

Public Attributes

T * data
 
size_t pitch
 
int w
 
int h
 
int count
 

Detailed Description

template<typename T>
struct cudaImageList< T >

Stores a large number of small images into a single large memory space. Optimizes GPU memory copies. It has no constructor/destructor, so it can be passed to CUDA kernels. The memory is allocated using cudaMallocPitch. It allows binding to a texture.

Todo:
Maybe this should be converted into a 3D cudaArray?

Definition at line 10 of file cudaImageList.h.

Member Enumeration Documentation

§ anonymous enum

template<typename T>
anonymous enum
Enumerator
MaxImageWidth 

Definition at line 19 of file cudaImageList.h.

Member Function Documentation

§ alloc()

template<typename T>
static cudaImageList<T> cudaImageList< T >::alloc ( int  w,
int  h,
int  amount 
)
inlinestatic

Definition at line 35 of file cudaImageList.h.

35  {
36  cudaImageList imgl;
37  imgl.w = w; imgl.h = h;
38  imgl.count = amount;
39 
40  if (cudaMallocPitch(&imgl.data, &imgl.pitch, sizeof(T)*imgl.fullwidth(), imgl.fullheight()) != cudaSuccess) {
41  throw std::bad_alloc(SPrintf("cudaImageListf<%s> alloc %dx%dx%d failed", typeid(T).name(), w, h, amount).c_str());
42  }
43  return imgl;
44  }
Stores a large number of small images into a single large memory space. Optimizes GPU memory copies...
Definition: cudaImageList.h:10
CUBOTH int fullwidth()
Definition: cudaImageList.h:16
CUBOTH int fullheight()
Definition: cudaImageList.h:17
std::string SPrintf(const char *fmt,...)
Definition: utils.cpp:132

§ allocateHostImageBuffer()

template<typename T>
template<int Flags>
void cudaImageList< T >::allocateHostImageBuffer ( pinned_array< T, Flags > &  hostImgBuf)
inline

Definition at line 47 of file cudaImageList.h.

47  {
48  hostImgBuf.init( numpixels() );
49  }
CUBOTH int numpixels()
Definition: cudaImageList.h:22
void init(int n)
Definition: gpu_utils.h:232

§ bind()

template<typename T>
void cudaImageList< T >::bind ( texture< T, cudaTextureType2D, cudaReadModeElementType > &  texref)
inline

Definition at line 166 of file cudaImageList.h.

166  {
167  cudaChannelFormatDesc desc = cudaCreateChannelDesc<T>();
168  cudaBindTexture2D(NULL, &texref, data, &desc, w, h*count, pitch);
169  }

§ boundaryHit()

template<typename T>
CUBOTH bool cudaImageList< T >::boundaryHit ( float2  center,
float  radius 
)
inline

Definition at line 79 of file cudaImageList.h.

80  {
81  return center.x + radius >= w ||
82  center.x - radius < 0 ||
83  center.y + radius >= h ||
84  center.y - radius < 0;
85  }

§ capacity()

template<typename T>
CUBOTH int cudaImageList< T >::capacity ( )
inline

Definition at line 21 of file cudaImageList.h.

21 { return count; }

§ clear()

template<typename T>
void cudaImageList< T >::clear ( )
inline

Definition at line 136 of file cudaImageList.h.

136  {
137  if(data) cudaMemset2D(data, pitch, 0, w*sizeof(T), count*h);
138  }

§ computeImagePos()

template<typename T>
CUBOTH void cudaImageList< T >::computeImagePos ( int &  x,
int &  y,
int  idx 
)
inline

Definition at line 174 of file cudaImageList.h.

175  {
176  y += idx * h;
177  }

§ copyImageToDevice()

template<typename T>
void cudaImageList< T >::copyImageToDevice ( int  img,
T *  src,
bool  async = false,
cudaStream_t  s = 0 
)
inline

Definition at line 106 of file cudaImageList.h.

106  {
107  T* dst = pixelAddress (0,0, img);
108 
109  if (async)
110  cudaMemcpy2DAsync(dst, pitch, src, w*sizeof(T), w*sizeof(T), h, cudaMemcpyHostToDevice, s);
111  else
112  cudaMemcpy2D(dst, pitch, src, w*sizeof(T), w*sizeof(T), h, cudaMemcpyHostToDevice);
113  }
CUBOTH T * pixelAddress(int x, int y, int imgIndex)
Definition: cudaImageList.h:71

§ copyImageToHost()

template<typename T>
void cudaImageList< T >::copyImageToHost ( int  img,
T *  dst,
bool  async = false,
cudaStream_t  s = 0 
)
inline

Definition at line 97 of file cudaImageList.h.

97  {
98  T* src = pixelAddress (0,0, img);
99 
100  if (async)
101  cudaMemcpy2DAsync(dst, sizeof(T)*w, src, pitch, w*sizeof(T), h, cudaMemcpyDeviceToHost, s);
102  else
103  cudaMemcpy2D(dst, sizeof(T)*w, src, pitch, w*sizeof(T), h, cudaMemcpyDeviceToHost);
104  }
CUBOTH T * pixelAddress(int x, int y, int imgIndex)
Definition: cudaImageList.h:71

§ copyToDevice() [1/2]

template<typename T>
void cudaImageList< T >::copyToDevice ( T *  src,
bool  async = false,
cudaStream_t  s = 0 
)
inline

Definition at line 122 of file cudaImageList.h.

122  {
123  if (async)
124  cudaMemcpy2DAsync(data, pitch, src, w*sizeof(T), w*sizeof(T), count*h, cudaMemcpyHostToDevice, s);
125  else
126  cudaMemcpy2D(data, pitch, src, w*sizeof(T), w*sizeof(T), count*h, cudaMemcpyHostToDevice);
127  }

§ copyToDevice() [2/2]

template<typename T>
void cudaImageList< T >::copyToDevice ( T *  src,
int  numImages,
bool  async = false,
cudaStream_t  s = 0 
)
inline

Definition at line 129 of file cudaImageList.h.

129  {
130  if (async)
131  cudaMemcpy2DAsync(data, pitch, src, w*sizeof(T), w*sizeof(T), numImages*h, cudaMemcpyHostToDevice, s);
132  else
133  cudaMemcpy2D(data, pitch, src, w*sizeof(T), w*sizeof(T), numImages*h, cudaMemcpyHostToDevice);
134  }

§ copyToHost()

template<typename T>
void cudaImageList< T >::copyToHost ( T *  dst,
bool  async = false,
cudaStream_t  s = 0 
)
inline

Definition at line 115 of file cudaImageList.h.

115  {
116  if (async)
117  cudaMemcpy2DAsync(dst, sizeof(T)*w, data, pitch, w*sizeof(T), count*h, cudaMemcpyDeviceToHost, s);
118  else
119  cudaMemcpy2D(dst, sizeof(T)*w, data, pitch, w*sizeof(T), count*h, cudaMemcpyDeviceToHost);
120  }

§ emptyList()

template<typename T>
static cudaImageList<T> cudaImageList< T >::emptyList ( )
inlinestatic

Definition at line 24 of file cudaImageList.h.

24  {
25  cudaImageList imgl;
26  imgl.data = 0;
27  imgl.pitch = 0;
28  imgl.w = imgl.h = 0;
29  imgl.count = 0;
30  return imgl;
31  }
Stores a large number of small images into a single large memory space. Optimizes GPU memory copies...
Definition: cudaImageList.h:10

§ free()

template<typename T>
void cudaImageList< T >::free ( )
inline

Definition at line 88 of file cudaImageList.h.

89  {
90  if(data) {
91  cudaFree(data);
92  data=0;
93  }
94  }

§ fullheight()

template<typename T>
CUBOTH int cudaImageList< T >::fullheight ( )
inline

Definition at line 17 of file cudaImageList.h.

17 { return h*count; }

§ fullwidth()

template<typename T>
CUBOTH int cudaImageList< T >::fullwidth ( )
inline

Definition at line 16 of file cudaImageList.h.

16 { return w; }

§ get()

template<typename T>
CUBOTH T* cudaImageList< T >::get ( int  i)
inline

Definition at line 51 of file cudaImageList.h.

51  {
52  return (T*)(((char*)data) + pitch*h*i);
53  }

§ interp()

template<typename T>
static CUBOTH T cudaImageList< T >::interp ( a,
b,
float  x 
)
inlinestatic

Definition at line 143 of file cudaImageList.h.

143 { return a + (b-a)*x; }

§ interpolate()

template<typename T>
CUBOTH T cudaImageList< T >::interpolate ( float  x,
float  y,
int  idx,
bool &  outside 
)
inline

Definition at line 145 of file cudaImageList.h.

146  {
147  int rx=x, ry=y;
148 
149  if (rx < 0 || ry < 0 || rx >= w-1 || ry >= h-1) {
150  outside=true;
151  return 0.0f;
152  }
153 
154  T v00 = pixel(rx, ry, idx);
155  T v10 = pixel(rx+1, ry, idx);
156  T v01 = pixel(rx, ry+1, idx);
157  T v11 = pixel(rx+1, ry+1, idx);
158 
159  T v0 = interp (v00, v10, x-rx);
160  T v1 = interp (v01, v11, x-rx);
161 
162  outside=false;
163  return interp (v0, v1, y-ry);
164  }
CUBOTH T & pixel(int x, int y, int imgIndex)
Definition: cudaImageList.h:64
static CUBOTH T interp(T a, T b, float x)

§ interpolateFromTexture()

template<typename T>
__device__ T cudaImageList< T >::interpolateFromTexture ( texture< T, cudaTextureType2D, cudaReadModeElementType >  texref,
float  x,
float  y,
int  idx,
bool &  outside 
)
inline

Definition at line 180 of file cudaImageList.h.

181  {
182  int rx=x, ry=y;
183 
184  if (rx < 0 || ry < 0 || rx >= w-1 || ry >= h-1) {
185  outside=true;
186  return 0.0f;
187  }
188 
189  computeImagePos(rx, ry, idx);
190 
191  float fx=x-floor(x), fy = y-floor(y);
192  float u = rx + 0.5f;
193  float v = ry + 0.5f;
194 
195  T v00 = tex2D(texref, u, v);
196  T v10 = tex2D(texref, u+1, v);
197  T v01 = tex2D(texref, u, v+1);
198  T v11 = tex2D(texref, u+1, v+1);
199 
200  T v0 = interp (v00, v10, fx);
201  T v1 = interp (v01, v11, fx);
202 
203  outside = false;
204  return interp (v0, v1, fy);
205  }
CUBOTH void computeImagePos(int &x, int &y, int idx)
static CUBOTH T interp(T a, T b, float x)

§ isEmpty()

template<typename T>
CUBOTH bool cudaImageList< T >::isEmpty ( )
inline

Definition at line 33 of file cudaImageList.h.

33 { return data==0; }

§ numpixels()

template<typename T>
CUBOTH int cudaImageList< T >::numpixels ( )
inline

Definition at line 22 of file cudaImageList.h.

22 { return w*h*count; }

§ pixel()

template<typename T>
CUBOTH T& cudaImageList< T >::pixel ( int  x,
int  y,
int  imgIndex 
)
inline

Definition at line 64 of file cudaImageList.h.

64  {
65  computeImagePos(x,y,imgIndex);
66  T* row = (T*) ( (char*)data + y*pitch );
67  return row[x];
68  }
CUBOTH void computeImagePos(int &x, int &y, int idx)

§ pixel_oobcheck()

template<typename T>
CUBOTH T cudaImageList< T >::pixel_oobcheck ( int  x,
int  y,
int  imgIndex,
border = 0.0f 
)
inline

Definition at line 55 of file cudaImageList.h.

55  {
56  if (x < 0 || x >= w || y < 0 || y >= h)
57  return border;
58 
59  computeImagePos(x,y,imgIndex);
60  T* row = (T*) ( (char*)data + y*pitch );
61  return row[x];
62  }
CUBOTH void computeImagePos(int &x, int &y, int idx)

§ pixelAddress()

template<typename T>
CUBOTH T* cudaImageList< T >::pixelAddress ( int  x,
int  y,
int  imgIndex 
)
inline
Bug:
Possibly bugged, should be row + x * sizeof(T)? X is always 0 though, so never encountered.

Definition at line 71 of file cudaImageList.h.

71  {
72  computeImagePos(x,y,imgIndex);
73  T* row = (T*) ( (char*)data + y*pitch );
74  return row + x;
75  }
CUBOTH void computeImagePos(int &x, int &y, int idx)

§ totalNumBytes()

template<typename T>
CUBOTH int cudaImageList< T >::totalNumBytes ( )
inline

Definition at line 141 of file cudaImageList.h.

141 { return w*h*count*sizeof(T); }

§ totalNumPixels()

template<typename T>
CUBOTH int cudaImageList< T >::totalNumPixels ( )
inline

Definition at line 140 of file cudaImageList.h.

140 { return w*h*count; }

§ unbind()

template<typename T>
void cudaImageList< T >::unbind ( texture< T, cudaTextureType2D, cudaReadModeElementType > &  texref)
inline

Definition at line 170 of file cudaImageList.h.

170  {
171  cudaUnbindTexture(&texref);
172  }

Member Data Documentation

§ count

template<typename T>
int cudaImageList< T >::count

Definition at line 14 of file cudaImageList.h.

§ data

template<typename T>
T* cudaImageList< T >::data

Definition at line 11 of file cudaImageList.h.

§ h

template<typename T>
int cudaImageList< T >::h

Definition at line 13 of file cudaImageList.h.

§ pitch

template<typename T>
size_t cudaImageList< T >::pitch

Definition at line 12 of file cudaImageList.h.

§ w

template<typename T>
int cudaImageList< T >::w

Definition at line 13 of file cudaImageList.h.


The documentation for this struct was generated from the following file: