HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CE_Image.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  */
7 
8 #ifndef __CE_IMAGE__
9 #define __CE_IMAGE__
10 
11 #include "CE_API.h"
12 
13 #include <UT/UT_NonCopyable.h>
14 #include <UT/UT_VectorTypes.h>
15 #include <UT/UT_VoxelArray.h>
16 
17 #include <SYS/SYS_Inline.h>
18 #include <SYS/SYS_Types.h>
19 
20 /// This class represents OpenCL storage of an image (a 2-dimensional
21 /// rectangular grid of pixels with 1-4 floating point channels).
23 {
24 public:
25  /// Type of data stored in this buffer. This enum must be in sync with
26  /// storage types defined in the imx.h OpenCL header.
28  {
29  INT8 = 0,
35  FIXED16
36  };
37  static const char* getName(StorageType);
38  static StorageType storageTypeFromName(const char *name);
39  /// String used in snippets to identify the storage type.
40  static const char* getStorageString(StorageType);
41  static bool storesIntegers(StorageType t) { return t < FLOAT16; }
42  static bool isFixedPoint(StorageType t) { return t >= FIXED8; }
43  static int getBytes(StorageType t)
44  { return (t == INT8 || t == FIXED8) ? 1 :
45  ((t == INT16 || t == FLOAT16 || t == FIXED16) ? 2 : 4); }
46 
47 public:
48  /// Creates a new uninitialized object. Call setSize() to allocate memory.
49  CE_Image();
50  ~CE_Image();
51 
53 
54  /// Copies data into this image from other. If sizes are different between
55  /// the two,
56  /// - nothing is done if force is false,
57  /// - this buffer is resized, then the copy performed if force is true.
58  /// Returns true if copying was done, false otherwise.
59  bool copy(const CE_Image& other, bool force = false);
60 
61  /// Swaps the backing buffers between the two objects.
62  void swap(CE_Image& other);
63 
64  /// Returns true if this buffer is allocated and usable.
65  SYS_FORCE_INLINE bool isValid() const
66  {
67  return myBuffer() != 0;
68  }
69 
70  /// Resizes this buffer; allocated memory is uninitialized.
71  void setSize(int width, int height, int channels,
72  StorageType storage);
73 
74  /// Resizes this buffer to match the other image. Allocated memory is not
75  /// initialized.
76  void match(const CE_Image& other);
77 
78  /// Resizes this buffer to match the voxel array and copies its data into
79  /// the image. Z-resolution of src should be 1.
80  /// Number of channels is set based on T (which can be float, UT_Vector2F,
81  /// UT_Vector3F, UT_Vector4F, or int64).
82  template <typename T>
83  void initFromVoxels(const UT_VoxelArray<T>& src);
84 
85  /// Releases memory held by this buffer. setSize() must be called to make
86  /// this object usable again.
87  void destroy();
88 
89  /// Upload data into this image from src. If blocking is true, the copying
90  /// will be complete by the time the function returns; otherwise, the
91  /// operation is simply queued up.
92  void readIn(const void* src, bool blocking = true);
93 
94  /// Download data from this image into dst. If blocking is true, the copying
95  /// will be complete by the time the function returns; otherwise, the
96  /// operation is only queued up.
97  void writeOut(void* dst, bool blocking = true) const;
98 
99  /// Resizes dest to match dimensions of this image (its Z-resolution is set
100  /// to 1). Number of channels in this image should equal tuple size of T.
101  /// This function also downloads the image's data into dest.
102  template <typename T>
103  void matchAndCopyToVoxels(UT_VoxelArray<T>& dest) const;
104 
105  /// Returns this buffer descriptor.
106  const cl::Buffer& buffer() const
107  {
108  return myBuffer;
109  }
110 
111  /// Total memory in bytes needed to hold this image's data.
112  int64 totalMemory() const;
113  /// Total memory in bytes needed to hold an image with the given properties.
114  static int64 totalMemory(exint width, exint height, exint channels,
115  StorageType storage);
116 
117  /// Returns the image's width.
118  int getWidth() const
119  {
120  return myWidth;
121  }
122 
123  /// Returns the image's height.
124  int getHeight() const
125  {
126  return myHeight;
127  }
128 
129  /// Returns the number of channels per pixel.
130  int getChannels() const
131  {
132  return myChannels;
133  }
134 
135  /// Identifies the type of data stored for each channel of every pixel.
137  {
138  return myStorage;
139  }
140 
141  /// Bind a 2D kernel with one work item per pixel.
142  cl::KernelFunctor bind(cl::Kernel k) const;
143 
144  /// Sets this buffer to the given constant value.
145  void setValue(const UT_Vector4F& val);
146  void setValue(int val);
147 
148  /// Converts the given voxel coordinates to the normalized [0..1]^3 space in
149  /// the same way as done in UT_VoxelArray.
150  bool indexToPos(int x, int y, int z, UT_Vector3F& pos) const
151  {
152  return indexToPos(x, y, z, pos, myWidth, myHeight);
153  }
154  /// Converts the given voxel coordinates to the normalized [0..1]^3 space in
155  /// the same way as done in UT_VoxelArray. This static version of the method
156  /// must be provided with width and height.
157  static bool indexToPos(int x, int y, int z, UT_Vector3F& pos, int w, int h);
158 
159 protected:
160  /// OpenCL descriptor for the actual GPU buffer.
162  /// Sizes of the buffer.
163  int myWidth;
164  int myHeight;
167 };
168 
169 /// This function can be used to convert a flat array (of specified storage)
170 /// into a 2D voxel array of vectors, integers, or floats. The destination voxel
171 /// array must have its size already initialized. src must be of size
172 /// X_RES * Y_RES * TUPLE_SIZE_OF_T * BYTES_FOR_STORAGE
173 /// and its layout must be {layers of x-pixels}, where each is {pixel data}, and
174 /// each one of those is {component0, component1, ...} (every component's type
175 /// is controlled by the storage specifier), everything tightly packed.
176 template <typename T>
178  const void* src,
180 /// This function can be used to convert a 2D voxel array of vectors, integers,
181 /// or floats into a flat array of specified storage. The source array must have
182 /// Z-resolution of 1. dest must be of size
183 /// X_RES * Y_RES * TUPLE_SIZE_OF_T * BYTES_FOR_STORAGE
184 /// and its final layout will be
185 /// {layers of y-pixels: {layers of x-pixels: {comp0, comp1, ...}}}
186 /// That is, every component of every pixel along the first row, followed by the
187 /// second row, etc.
188 template <typename T>
190  const UT_VoxelArray<T>& src,
192 
193 /// Type traits for a given storage type...
194 template <CE_Image::StorageType STORAGE>
196 {
197  typedef void DataType;
198  static const int DataSize = 0;
199 };
200 // ...and its concrete specializations.
201 template <>
203 {
204  typedef short DataType;
205  static const int DataSize = sizeof(DataType);
206 };
207 template <>
209 {
210  typedef unsigned char DataType;
211  static const int DataSize = sizeof(DataType);
212 };
213 template <>
215 {
217  static const int DataSize = sizeof(DataType);
218 };
219 template <>
221 {
223  static const int DataSize = sizeof(DataType);
224 };
225 template <>
227 {
228  typedef int DataType;
229  static const int DataSize = sizeof(DataType);
230 };
231 template <>
233 {
234  typedef short DataType;
235  static const int DataSize = sizeof(DataType);
236 };
237 template <>
239 {
240  typedef unsigned char DataType;
241  static const int DataSize = sizeof(DataType);
242 };
243 
244 /******************************************************************************
245  * DATA CONVERTERS
246  ******************************************************************************/
247 
248 /// Templated functions for converting to and from values that are stored in a
249 /// CE_Image (storage-aware).
250 template <CE_Image::StorageType STORAGE, typename SCALAR_TYPE>
252 {
254 
256  toCEImage(SCALAR_TYPE v)
257  {
258  return CE_SCALAR_TYPE(v);
259  }
260  static SYS_FORCE_INLINE SCALAR_TYPE
262  {
263  return SCALAR_TYPE(v);
264  }
265 };
266 /// Fixed points specialization for 8-bit.
267 template <typename SCALAR_TYPE>
268 struct CE_ImageValueConverter<CE_Image::StorageType::FIXED8, SCALAR_TYPE>
269 {
270  static constexpr CE_Image::StorageType STORAGE =
271  CE_Image::StorageType::FIXED8;
273  static constexpr CE_SCALAR_TYPE ONE_POINT =
275 
277  toCEImage(SCALAR_TYPE v)
278  {
279  return CE_SCALAR_TYPE(SYSclamp(v, SCALAR_TYPE(0), SCALAR_TYPE(1))
280  * ONE_POINT);
281  }
282  static SYS_FORCE_INLINE SCALAR_TYPE
284  {
285  return SCALAR_TYPE(v) / SCALAR_TYPE(ONE_POINT);
286  }
287 };
288 /// Fixed points specialization for 16-bit.
289 template <typename SCALAR_TYPE>
290 struct CE_ImageValueConverter<CE_Image::StorageType::FIXED16, SCALAR_TYPE>
291 {
292  static constexpr CE_Image::StorageType STORAGE =
293  CE_Image::StorageType::FIXED16;
295  static constexpr CE_SCALAR_TYPE ONE_POINT =
297 
299  toCEImage(SCALAR_TYPE v)
300  {
301  return CE_SCALAR_TYPE(SYSclamp(v, SCALAR_TYPE(-1), SCALAR_TYPE(1))
302  * ONE_POINT);
303  }
304  static SYS_FORCE_INLINE SCALAR_TYPE
306  {
307  return SCALAR_TYPE(v) / SCALAR_TYPE(ONE_POINT);
308  }
309 };
310 
311 /// Templated function for converting between different storage types.
312 template <CE_Image::StorageType FROM, CE_Image::StorageType TO>
314 {
319 
322  {
325  }
326 };
327 /// Optimized specialization to forego all conversion if source and destination
328 /// storage types are the same.
329 template <CE_Image::StorageType STORAGE>
330 struct CE_ImageDataConverter<STORAGE, STORAGE>
331 {
334 
337  {
338  return v;
339  }
340 };
341 
342 #endif
343 
#define CE_API
Definition: CE_API.h:13
static SYS_FORCE_INLINE CE_SCALAR_TYPE toCEImage(SCALAR_TYPE v)
Definition: CE_Image.h:277
static SYS_FORCE_INLINE SCALAR_TYPE fromCEImage(CE_SCALAR_TYPE v)
Definition: CE_Image.h:305
typename CE_StorageTypeTraits< STORAGE >::DataType FROM_SCALAR_TYPE
Definition: CE_Image.h:332
static bool storesIntegers(StorageType t)
Definition: CE_Image.h:41
static bool isFixedPoint(StorageType t)
Definition: CE_Image.h:42
getFileOption("OpenEXR:storage") storage
Definition: HDK_Image.dox:276
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
const GLdouble * v
Definition: glcorearb.h:837
typename CE_StorageTypeTraits< STORAGE >::DataType CE_SCALAR_TYPE
Definition: CE_Image.h:294
typename CE_StorageTypeTraits< TO >::DataType TO_SCALAR_TYPE
Definition: CE_Image.h:316
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
int64 exint
Definition: SYS_Types.h:125
void swap(T &lhs, T &rhs)
Definition: pugixml.cpp:7440
static SYS_FORCE_INLINE TO_SCALAR_TYPE convert(FROM_SCALAR_TYPE v)
Definition: CE_Image.h:336
int getWidth() const
Returns the image's width.
Definition: CE_Image.h:118
GLint y
Definition: glcorearb.h:103
__hostdev__ void setValue(uint32_t offset, bool v)
Definition: NanoVDB.h:5750
float fpreal32
Definition: SYS_Types.h:200
OutGridT const XformOp bool bool
bool indexToPos(int x, int y, int z, UT_Vector3F &pos) const
Definition: CE_Image.h:150
int getChannels() const
Returns the number of channels per pixel.
Definition: CE_Image.h:130
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
Type traits for a given storage type...
Definition: CE_Image.h:195
static SYS_FORCE_INLINE CE_SCALAR_TYPE toCEImage(SCALAR_TYPE v)
Definition: CE_Image.h:299
void CE_API CEfillFlatStorageFromVoxelArray(void *dest, const UT_VoxelArray< T > &src, CE_Image::StorageType storage)
typename CE_StorageTypeTraits< FROM >::DataType FROM_SCALAR_TYPE
Definition: CE_Image.h:315
void CE_API CEfillVoxelArrayFromFlatStorage(UT_VoxelArray< T > &dest, const void *src, CE_Image::StorageType storage)
PXL_API const char * getName(const ColorSpace *space)
Return the name of the color space.
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
UT_Vector3T< T > SYSclamp(const UT_Vector3T< T > &v, const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
Definition: UT_Vector3.h:1057
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
cl::Buffer myBuffer
OpenCL descriptor for the actual GPU buffer.
Definition: CE_Image.h:161
GLuint const GLchar * name
Definition: glcorearb.h:786
int getHeight() const
Returns the image's height.
Definition: CE_Image.h:124
GLint GLenum GLint x
Definition: glcorearb.h:409
typename CE_StorageTypeTraits< STORAGE >::DataType CE_SCALAR_TYPE
Definition: CE_Image.h:272
GLdouble t
Definition: glad.h:2397
static const int DataSize
Definition: CE_Image.h:198
static SYS_FORCE_INLINE SCALAR_TYPE fromCEImage(CE_SCALAR_TYPE v)
Definition: CE_Image.h:261
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
GLenum GLenum dst
Definition: glcorearb.h:1793
SIM_API const UT_StringHolder force
static SYS_FORCE_INLINE SCALAR_TYPE fromCEImage(CE_SCALAR_TYPE v)
Definition: CE_Image.h:283
StorageType
Definition: CE_Image.h:27
int myWidth
Sizes of the buffer.
Definition: CE_Image.h:163
StorageType myStorage
Definition: CE_Image.h:166
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLuint GLfloat * val
Definition: glcorearb.h:1608
Kernel functor interface.
Definition: cl.hpp:3585
static SYS_FORCE_INLINE TO_SCALAR_TYPE convert(FROM_SCALAR_TYPE v)
Definition: CE_Image.h:321
Memory buffer interface.
Definition: cl.hpp:1867
GLint GLsizei width
Definition: glcorearb.h:103
static int getBytes(StorageType t)
Definition: CE_Image.h:43
Kernel interface that implements cl_kernel.
Definition: cl.hpp:2544
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
StorageType getStorage() const
Identifies the type of data stored for each channel of every pixel.
Definition: CE_Image.h:136
static SYS_FORCE_INLINE CE_SCALAR_TYPE toCEImage(SCALAR_TYPE v)
Definition: CE_Image.h:256
typename CE_StorageTypeTraits< STORAGE >::DataType CE_SCALAR_TYPE
Definition: CE_Image.h:253
const cl::Buffer & buffer() const
Returns this buffer descriptor.
Definition: CE_Image.h:106
Templated function for converting between different storage types.
Definition: CE_Image.h:313
ImageBuf OIIO_API channels(const ImageBuf &src, int nchannels, cspan< int > channelorder, cspan< float > channelvalues={}, cspan< std::string > newchannelnames={}, bool shuffle_channel_names=false, int nthreads=0)
int myHeight
Definition: CE_Image.h:164
GLenum src
Definition: glcorearb.h:1793
typename CE_StorageTypeTraits< STORAGE >::DataType TO_SCALAR_TYPE
Definition: CE_Image.h:333
int myChannels
Definition: CE_Image.h:165