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 
37 public:
38  /// Creates a new uninitialized object. Call setSize() to allocate memory.
39  CE_Image();
40  ~CE_Image();
41 
43 
44  /// Copies data into this image from other. If sizes are different between
45  /// the two,
46  /// - nothing is done if force is false,
47  /// - this buffer is resized, then the copy performed if force is true.
48  /// Returns true if copying was done, false otherwise.
49  bool copy(const CE_Image& other, bool force = false);
50 
51  /// Swaps the backing buffers between the two objects.
52  void swap(CE_Image& other);
53 
54  /// Returns true if this buffer is allocated and usable.
55  SYS_FORCE_INLINE bool isValid() const
56  {
57  return myBuffer() != 0;
58  }
59 
60  /// Resizes this buffer; allocated memory is uninitialized.
61  void setSize(int width, int height, int channels,
62  StorageType storage);
63 
64  /// Resizes this buffer to match the other image. Allocated memory is not
65  /// initialized.
66  void match(const CE_Image& other);
67 
68  /// Resizes this buffer to match the voxel array and copies its data into
69  /// the image. Z-resolution of src should be 1.
70  /// Number of channels is set based on T (which can be float, UT_Vector2F,
71  /// UT_Vector3F, UT_Vector4F, or int64).
72  template <typename T>
73  void initFromVoxels(const UT_VoxelArray<T>& src);
74 
75  /// Releases memory held by this buffer. setSize() must be called to make
76  /// this object usable again.
77  void destroy();
78 
79  /// Upload data into this image from src. If blocking is true, the copying
80  /// will be complete by the time the function returns; otherwise, the
81  /// operation is simply queued up.
82  void readIn(const void* src, bool blocking = true);
83 
84  /// Download data from this image into dst. If blocking is true, the copying
85  /// will be complete by the time the function returns; otherwise, the
86  /// operation is only queued up.
87  void writeOut(void* dst, bool blocking = true) const;
88 
89  /// Resizes dest to match dimensions of this image (its Z-resolution is set
90  /// to 1). Number of channels in this image should equal tuple size of T.
91  /// This function also downloads the image's data into dest.
92  template <typename T>
93  void matchAndCopyToVoxels(UT_VoxelArray<T>& dest) const;
94 
95  /// Returns this buffer descriptor.
96  const cl::Buffer& buffer() const
97  {
98  return myBuffer;
99  }
100 
101  /// Total memory in bytes needed to hold this image's data.
102  int64 totalMemory() const;
103 
104  /// Returns the image's width.
105  int getWidth() const
106  {
107  return myWidth;
108  }
109 
110  /// Returns the image's height.
111  int getHeight() const
112  {
113  return myHeight;
114  }
115 
116  /// Returns the number of channels per pixel.
117  int getChannels() const
118  {
119  return myChannels;
120  }
121 
122  /// Identifies the type of data stored for each channel of every pixel.
124  {
125  return myStorage;
126  }
127 
128  /// Bind a 2D kernel with one work item per pixel.
129  cl::KernelFunctor bind(cl::Kernel k) const;
130 
131  /// Sets this buffer to the given constant value.
132  void setValue(const UT_Vector4F& val);
133 
134  /// Converts the given voxel coordinates to the normalized [0..1]^3 space in
135  /// the same way as done in UT_VoxelArray.
136  bool indexToPos(int x, int y, int z, UT_Vector3F& pos) const;
137 
138 protected:
139  /// OpenCL descriptor for the actual GPU buffer.
141  /// Sizes of the buffer.
142  int myWidth;
143  int myHeight;
146 };
147 
148 /// Type traits for a given storage type...
149 template <CE_Image::StorageType STORAGE>
151 {
152  typedef void DataType;
153  static const int DataSize = 0;
154 };
155 // ...and its concrete specializations.
156 template <>
158 {
160  static const int DataSize = sizeof(DataType);
161 };
162 template <>
164 {
166  static const int DataSize = sizeof(DataType);
167 };
168 template <>
170 {
171  typedef int DataType;
172  static const int DataSize = sizeof(DataType);
173 };
174 template <>
176 {
177  typedef short DataType;
178  static const int DataSize = sizeof(DataType);
179 };
180 template <>
182 {
183  typedef char DataType;
184  static const int DataSize = sizeof(DataType);
185 };
186 
187 #endif
188 
#define CE_API
Definition: CE_API.h:10
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
void swap(T &lhs, T &rhs)
Definition: pugixml.cpp:7172
int getWidth() const
Returns the image's width.
Definition: CE_Image.h:105
GLint y
Definition: glcorearb.h:103
float fpreal32
Definition: SYS_Types.h:200
int getChannels() const
Returns the number of channels per pixel.
Definition: CE_Image.h:117
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
Type traits for a given storage type...
Definition: CE_Image.h:150
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:140
int getHeight() const
Returns the image's height.
Definition: CE_Image.h:111
GLint GLenum GLint x
Definition: glcorearb.h:409
static const int DataSize
Definition: CE_Image.h:153
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:142
StorageType myStorage
Definition: CE_Image.h:145
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
Kernel interface that implements cl_kernel.
Definition: cl.hpp:2544
StorageType getStorage() const
Identifies the type of data stored for each channel of every pixel.
Definition: CE_Image.h:123
#define const
Definition: zconf.h:214
const cl::Buffer & buffer() const
Returns this buffer descriptor.
Definition: CE_Image.h:96
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:143
GLenum src
Definition: glcorearb.h:1793
int myChannels
Definition: CE_Image.h:144