ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkGPUImage.h
Go to the documentation of this file.
1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18 
19 #ifndef __itkGPUImage_h
20 #define __itkGPUImage_h
21 
22 #include "itkImage.h"
23 #include "itkGPUImageDataManager.h"
24 #include "itkVersion.h"
25 #include "itkObjectFactoryBase.h"
26 
27 namespace itk
28 {
39 template <class TPixel, unsigned int VImageDimension = 2>
40 class ITK_EXPORT GPUImage : public Image<TPixel,VImageDimension>
41 {
42 public:
43  typedef GPUImage Self;
48 
49  itkNewMacro(Self);
50 
51  itkTypeMacro(GPUImage, Image);
52 
53  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
54 
55  typedef typename Superclass::PixelType PixelType;
56  typedef typename Superclass::ValueType ValueType;
57  typedef typename Superclass::InternalPixelType InternalPixelType;
58  typedef typename Superclass::IOPixelType IOPixelType;
59  typedef typename Superclass::DirectionType DirectionType;
60  typedef typename Superclass::SpacingType SpacingType;
61  typedef typename Superclass::PixelContainer PixelContainer;
62  typedef typename Superclass::SizeType SizeType;
63  typedef typename Superclass::IndexType IndexType;
64  typedef typename Superclass::OffsetType OffsetType;
65  typedef typename Superclass::RegionType RegionType;
68  typedef typename Superclass::AccessorType AccessorType;
69 
71 
73  //typedef typename Superclass::NeighborhoodAccessorFunctorType
74  // NeighborhoodAccessorFunctorType;
75 
76  //
77  // Allocate CPU and GPU memory space
78  //
79  void Allocate();
80 
81  virtual void Initialize();
82 
83  void FillBuffer(const TPixel & value);
84 
85  void SetPixel(const IndexType & index, const TPixel & value);
86 
87  const TPixel & GetPixel(const IndexType & index) const;
88 
89  TPixel & GetPixel(const IndexType & index);
90 
91  const TPixel & operator[](const IndexType & index) const;
92 
93  TPixel & operator[](const IndexType & index);
94 
96  void UpdateBuffers();
97 
98  //
99  // Get CPU buffer pointer
100  //
101  TPixel* GetBufferPointer();
102 
103  const TPixel * GetBufferPointer() const;
104 
106  AccessorType GetPixelAccessor(void)
107  {
108  m_DataManager->SetGPUBufferDirty();
109  return Superclass::GetPixelAccessor();
110  }
112 
114  const AccessorType GetPixelAccessor(void) const
115  {
116  m_DataManager->UpdateCPUBuffer();
117  return Superclass::GetPixelAccessor();
118  }
120 
122  NeighborhoodAccessorFunctorType GetNeighborhoodAccessor()
123  {
124  m_DataManager->SetGPUBufferDirty();
125  //return Superclass::GetNeighborhoodAccessor();
127  }
129 
131  const NeighborhoodAccessorFunctorType GetNeighborhoodAccessor() const
132  {
133  m_DataManager->UpdateCPUBuffer();
134  //return Superclass::GetNeighborhoodAccessor();
136  }
138 
139  void SetPixelContainer(PixelContainer *container);
140 
142  PixelContainer * GetPixelContainer()
143  {
144  m_DataManager->SetGPUBufferDirty(); return Superclass::GetPixelContainer();
145  }
146 
147  const PixelContainer * GetPixelContainer() const
148  {
149  m_DataManager->UpdateCPUBuffer();
150  return Superclass::GetPixelContainer();
151  }
152 
153  void SetCurrentCommandQueue( int queueid )
154  {
155  m_DataManager->SetCurrentCommandQueue( queueid );
156  }
157 
159  return m_DataManager->GetCurrentCommandQueueID();
160  }
161 
162  itkGetModifiableObjectMacro(DataManager, GPUImageDataManager< GPUImage >);
163 
164  GPUDataManager::Pointer GetGPUDataManager() const;
165 
166  /* Override DataHasBeenGenerated() in DataObject class.
167  * We need this because CPU time stamp is always bigger
168  * than GPU's. That is because Modified() is called at
169  * the end of each filter in the pipeline so although we
170  * increment GPU's time stamp in GPUGenerateData() the
171  * CPU's time stamp will be increased after that.
172  */
173  void DataHasBeenGenerated()
174  {
175  Superclass::DataHasBeenGenerated();
176  if( m_DataManager->IsCPUBufferDirty() )
177  {
178  m_DataManager->Modified();
179  }
180  }
181 
183  virtual void Graft(const DataObject *data);
184 
186  virtual void Modified() const;
187 
188 protected:
189  GPUImage();
190  virtual ~GPUImage();
191 
192 private:
193 
194  // functions that are purposely not implemented
195  GPUImage(const Self&);
196  void operator=(const Self&);
197 
199 };
200 
202 {
203 public:
208 
210  virtual const char* GetITKSourceVersion() const {
211  return ITK_SOURCE_VERSION;
212  }
213  const char* GetDescription() const {
214  return "A Factory for GPUImage";
215  }
217 
219  itkFactorylessNewMacro(Self);
220 
223 
225  static void RegisterOneFactory(void)
226  {
228 
230  }
231 
232 private:
233  GPUImageFactory(const Self&); //purposely not implemented
234  void operator=(const Self&); //purposely not implemented
235 
236 #define OverrideImageTypeMacro(pt,dm) this->RegisterOverride( \
237  typeid(itk::Image<pt,dm>).name(), \
238  typeid(itk::GPUImage<pt,dm>).name(), \
239  "GPU Image Override", \
240  true, \
241  itk::CreateObjectFunction<GPUImage<pt,dm> >::New() )
242 
244  {
245  if( IsGPUAvailable() )
246  {
247  // 1/2/3D
248  OverrideImageTypeMacro(unsigned char, 1);
249  OverrideImageTypeMacro(signed char, 1);
250  OverrideImageTypeMacro(int, 1);
251  OverrideImageTypeMacro(unsigned int, 1);
252  OverrideImageTypeMacro(float, 1);
253  OverrideImageTypeMacro(double, 1);
254 
255  OverrideImageTypeMacro(unsigned char, 2);
256  OverrideImageTypeMacro(signed char, 2);
257  OverrideImageTypeMacro(int, 2);
258  OverrideImageTypeMacro(unsigned int, 2);
259  OverrideImageTypeMacro(float, 2);
260  OverrideImageTypeMacro(double, 2);
261 
262  OverrideImageTypeMacro(unsigned char, 3);
263  OverrideImageTypeMacro(signed char, 3);
264  OverrideImageTypeMacro(int, 3);
265  OverrideImageTypeMacro(unsigned int, 3);
266  OverrideImageTypeMacro(float, 3);
267  OverrideImageTypeMacro(double, 3);
268  }
269  }
270 
271 };
272 
273 template <class T>
275 {
276 public:
277  typedef T Type;
278 };
279 
280 template <class TPixelType, unsigned int NDimension>
281 class GPUTraits< Image< TPixelType, NDimension > >
282 {
283 public:
285 };
286 
287 } // end namespace itk
288 
289 #ifndef ITK_MANUAL_INSTANTIATION
290 #include "itkGPUImage.hxx"
291 #endif
292 
293 #endif
294