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,
34  };
35  static const char* getName(StorageType);
36  /// String used in snippets to identify the storage type.
37  static const char* getStorageString(StorageType);
38  static bool storesIntegers(StorageType t) { return t < FLOAT16; }
39  static int getBytes(StorageType t)
40  { return t == INT8 ? 1 : (t == INT16 || t == FLOAT16) ? 2 : 4; }
41 
42 public:
43  /// Creates a new uninitialized object. Call setSize() to allocate memory.
44  CE_Image();
45  ~CE_Image();
46 
48 
49  /// Copies data into this image from other. If sizes are different between
50  /// the two,
51  /// - nothing is done if force is false,
52  /// - this buffer is resized, then the copy performed if force is true.
53  /// Returns true if copying was done, false otherwise.
54  bool copy(const CE_Image& other, bool force = false);
55 
56  /// Swaps the backing buffers between the two objects.
57  void swap(CE_Image& other);
58 
59  /// Returns true if this buffer is allocated and usable.
60  SYS_FORCE_INLINE bool isValid() const
61  {
62  return myBuffer() != 0;
63  }
64 
65  /// Resizes this buffer; allocated memory is uninitialized.
66  void setSize(int width, int height, int channels,
67  StorageType storage);
68 
69  /// Resizes this buffer to match the other image. Allocated memory is not
70  /// initialized.
71  void match(const CE_Image& other);
72 
73  /// Resizes this buffer to match the voxel array and copies its data into
74  /// the image. Z-resolution of src should be 1.
75  /// Number of channels is set based on T (which can be float, UT_Vector2F,
76  /// UT_Vector3F, UT_Vector4F, or int64).
77  template <typename T>
78  void initFromVoxels(const UT_VoxelArray<T>& src);
79 
80  /// Releases memory held by this buffer. setSize() must be called to make
81  /// this object usable again.
82  void destroy();
83 
84  /// Upload data into this image from src. If blocking is true, the copying
85  /// will be complete by the time the function returns; otherwise, the
86  /// operation is simply queued up.
87  void readIn(const void* src, bool blocking = true);
88 
89  /// Download data from this image into dst. If blocking is true, the copying
90  /// will be complete by the time the function returns; otherwise, the
91  /// operation is only queued up.
92  void writeOut(void* dst, bool blocking = true) const;
93 
94  /// Resizes dest to match dimensions of this image (its Z-resolution is set
95  /// to 1). Number of channels in this image should equal tuple size of T.
96  /// This function also downloads the image's data into dest.
97  template <typename T>
98  void matchAndCopyToVoxels(UT_VoxelArray<T>& dest) const;
99 
100  /// Returns this buffer descriptor.
101  const cl::Buffer& buffer() const
102  {
103  return myBuffer;
104  }
105 
106  /// Total memory in bytes needed to hold this image's data.
107  int64 totalMemory() const;
108  /// Total memory in bytes needed to hold an image with the given properties.
109  static int64 totalMemory(exint width, exint height, exint channels,
110  StorageType storage);
111 
112  /// Returns the image's width.
113  int getWidth() const
114  {
115  return myWidth;
116  }
117 
118  /// Returns the image's height.
119  int getHeight() const
120  {
121  return myHeight;
122  }
123 
124  /// Returns the number of channels per pixel.
125  int getChannels() const
126  {
127  return myChannels;
128  }
129 
130  /// Identifies the type of data stored for each channel of every pixel.
132  {
133  return myStorage;
134  }
135 
136  /// Bind a 2D kernel with one work item per pixel.
137  cl::KernelFunctor bind(cl::Kernel k) const;
138 
139  /// Sets this buffer to the given constant value.
140  void setValue(const UT_Vector4F& val);
141  void setValue(int val);
142 
143  /// Converts the given voxel coordinates to the normalized [0..1]^3 space in
144  /// the same way as done in UT_VoxelArray.
145  bool indexToPos(int x, int y, int z, UT_Vector3F& pos) const
146  {
147  return indexToPos(x, y, z, pos, myWidth, myHeight);
148  }
149  /// Converts the given voxel coordinates to the normalized [0..1]^3 space in
150  /// the same way as done in UT_VoxelArray. This static version of the method
151  /// must be provided with width and height.
152  static bool indexToPos(int x, int y, int z, UT_Vector3F& pos, int w, int h);
153 
154 protected:
155  /// OpenCL descriptor for the actual GPU buffer.
157  /// Sizes of the buffer.
158  int myWidth;
159  int myHeight;
162 };
163 
164 /// This function can be used to convert a flat array (of specified storage)
165 /// into a 2D voxel array of vectors, integers, or floats. The destination voxel
166 /// array must have its size already initialized. src must be of size
167 /// X_RES * Y_RES * TUPLE_SIZE_OF_T * BYTES_FOR_STORAGE
168 /// and its layout must be {layers of x-pixels}, where each is {pixel data}, and
169 /// each one of those is {component0, component1, ...} (every component's type
170 /// is controlled by the storage specifier), everything tightly packed.
171 template <typename T>
173  const void* src,
175 /// This function can be used to convert a 2D voxel array of vectors, integers,
176 /// or floats into a flat array of specified storage. The source array must have
177 /// Z-resolution of 1. dest must be of size
178 /// X_RES * Y_RES * TUPLE_SIZE_OF_T * BYTES_FOR_STORAGE
179 /// and its final layout will be
180 /// {layers of y-pixels: {layers of x-pixels: {comp0, comp1, ...}}}
181 /// That is, every component of every pixel along the first row, followed by the
182 /// second row, etc.
183 template <typename T>
185  const UT_VoxelArray<T>& src,
187 
188 /// Type traits for a given storage type...
189 template <CE_Image::StorageType STORAGE>
191 {
192  typedef void DataType;
193  static const int DataSize = 0;
194 };
195 // ...and its concrete specializations.
196 template <>
198 {
200  static const int DataSize = sizeof(DataType);
201 };
202 template <>
204 {
206  static const int DataSize = sizeof(DataType);
207 };
208 template <>
210 {
211  typedef int DataType;
212  static const int DataSize = sizeof(DataType);
213 };
214 template <>
216 {
217  typedef short DataType;
218  static const int DataSize = sizeof(DataType);
219 };
220 template <>
222 {
223  typedef unsigned char DataType;
224  static const int DataSize = sizeof(DataType);
225 };
226 
227 #endif
228 
#define CE_API
Definition: CE_API.h:11
static bool storesIntegers(StorageType t)
Definition: CE_Image.h:38
getFileOption("OpenEXR:storage") storage
Definition: HDK_Image.dox:276
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
int64 exint
Definition: SYS_Types.h:125
void swap(T &lhs, T &rhs)
Definition: pugixml.cpp:7172
int getWidth() const
Returns the image's width.
Definition: CE_Image.h:113
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
bool indexToPos(int x, int y, int z, UT_Vector3F &pos) const
Definition: CE_Image.h:145
int getChannels() const
Returns the number of channels per pixel.
Definition: CE_Image.h:125
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
Type traits for a given storage type...
Definition: CE_Image.h:190
void CE_API CEfillFlatStorageFromVoxelArray(void *dest, const UT_VoxelArray< T > &src, CE_Image::StorageType storage)
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
#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:156
int getHeight() const
Returns the image's height.
Definition: CE_Image.h:119
GLint GLenum GLint x
Definition: glcorearb.h:409
GLdouble t
Definition: glad.h:2397
static const int DataSize
Definition: CE_Image.h:193
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
GLenum GLenum dst
Definition: glcorearb.h:1793
SIM_API const UT_StringHolder force
StorageType
Definition: CE_Image.h:27
int myWidth
Sizes of the buffer.
Definition: CE_Image.h:158
StorageType myStorage
Definition: CE_Image.h:161
GLuint GLfloat * val
Definition: glcorearb.h:1608
Kernel functor interface.
Definition: cl.hpp:3585
Memory buffer interface.
Definition: cl.hpp:1867
GLint GLsizei width
Definition: glcorearb.h:103
static int getBytes(StorageType t)
Definition: CE_Image.h:39
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:131
const cl::Buffer & buffer() const
Returns this buffer descriptor.
Definition: CE_Image.h:101
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:159
GLenum src
Definition: glcorearb.h:1793
int myChannels
Definition: CE_Image.h:160