HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IMX_Attribute.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  * NAME: IMX_Attribute.h
7  *
8  * COMMENTS:
9  * Holder for Detached Attributes, either on GPU or CPU
10  */
11 
12 #ifndef __IMX_Attribute_h__
13 #define __IMX_Attribute_h__
14 
15 #include "IMX_API.h"
16 #include "IMX_Types.h"
17 
18 #include <CE/CE_HotSwapPoolEntry.h>
19 #include <CE/CE_BufferHost.h>
20 
21 #include <GA/GA_CEAttribute.h>
22 #include <GA/GA_Types.h>
23 #include <GA/GA_Defaults.h>
24 #include <UT/UT_Lock.h>
25 #include <UT/UT_Options.h>
26 #include <UT/UT_SharedPtr.h>
27 #include <UT/UT_UniquePtr.h>
28 #include <UT/UT_Vector2.h>
29 #include <UT/UT_Vector3.h>
30 #include <UT/UT_Vector4.h>
31 #include <UT/UT_WorkBuffer.h>
32 #include <SYS/SYS_AtomicInt.h>
33 
34 class IMX_Attribute;
37 
38 class GU_Detail;
39 
41 {
42 public:
43  /// An un-allocated null-Attribute, but with default meta data.
44  IMX_Attribute();
45 
46  IMX_Attribute(const IMX_Attribute &other) { copy(other); }
47  IMX_Attribute(IMX_Attribute && other) noexcept { swap(other); }
48 
49  ~IMX_Attribute();
50 
51  /// Assignment operators
52  void copy(const IMX_Attribute &other);
53  IMX_Attribute &operator=(const IMX_Attribute &other) { copy(other); return *this; }
54 
55  void swap(IMX_Attribute &other);
56  IMX_Attribute &operator=(IMX_Attribute &&other) { swap(other); return *this; }
57 
58  /// Returns true for successful save.
59  bool save(std::ostream &os) const;
60  bool saveJSON(UT_JSONWriter &w) const;
61 
62  bool buildFromAttribute(const GA_Attribute *attrib, GA_AttributeOwner owner);
63 
64  /// If the storage/tuple match we get a direct copy, otherwise
65  /// zero init.
66  void buildFromEntries(GA_StorageClass storage,
67  int tuplesize,
68  const IMX_Attribute *src);
69  void buildFromEntries(GA_StorageClass storage,
70  int tuplesize,
72  GA_TypeInfo typeinfo,
73  bool isarray,
74  exint entries);
75 
76  /// Returns a newly created Attribute loaded from the stream, or null
77  /// if load failure.
78  static IMX_AttributePtr load(UT_IStream &is);
79  static IMX_AttributePtr loadJSON(UT_JSONParser &p);
80 
81  /// Write our fields into the provided options:
82  /// This does not include properties.
83  void copyMetadataToOptions(UT_Options &opt) const;
84  /// Load ourselves from the options, ignoring fields we don't know
85  void updateMetadataFromOptions(const UT_Options &opt);
86 
87  /// Builds an accessor to the underlying GA_CEAttribute.
88  /// NOTE: The GA_CEAttributeAccessor should have its lifetime go until
89  /// *after* the kernel has been dispatched.
90  GA_CEAttributeAccessorPtr getCEAttributeAccessor(bool read, bool write);
92  { return SYSconst_cast(this)->getCEAttributeAccessor(true, false); }
94  { return getCEAttributeAccessor(false, true); }
96  { return getCEAttributeAccessor(true, true); }
97 
98  // Lift from underlying attribute:
99  GA_Size entries() const { return myData ? myData->entries() : 0; }
100  int tupleSize() const { return myData ? myData->tupleSize() : 0; }
101  int getChannels() const { return tupleSize(); }
102  GA_StorageClass storage() const { return myData ? myData->storage() : GA_STORECLASS_INVALID; }
103  GA_Precision precision() const { return myData ? myData->precision() : GA_PRECISION_INVALID; }
104  bool isArray() const { return myData ? myData->isArray() : false; }
105 
106  void setTypeInfo(GA_TypeInfo typeinfo) { myTypeInfo = typeinfo; }
107  GA_TypeInfo typeInfo() const { return myTypeInfo; }
108 
109  void setDefaults(const GA_Defaults &def) { myDefaults = def; }
110  const GA_Defaults &defaults() const { return myDefaults; }
111 
112  /// The owner represents what they came from, that can be help
113  /// in debugging and tracking.
114  GA_AttributeOwner owner() const { return myOwner; }
115  void setOwner(GA_AttributeOwner owner) { myOwner = owner; }
116 
117  bool isOnGPU() const { return myData ? myData->isOnGPU() : false; }
118  bool isOnCPU() const { return myData ? myData->isOnCPU() : false; }
119 
120  /// In Use flags pass through to the underlying structure.
121  /// This is suspect, you should be holding an accessor instead.
122  void setInUseGPUFlag() const;
123  void clearInUseGPUFlag() const;
124 
125  /// The actual attribute data. This should be accessed via an accessor
126  /// normally but can be used to trigger sharing.
127  GA_CEAttributePtr data() { return myData; }
128  GA_CEAttributeConstPtr data() const { return myData; }
129 
130  /// True if data in this buffer may be stolen from (or changed) by the verbs
131  /// even if it's an input.
132  bool stealable() const { return myStealable; }
133 
134  /// Can be used to control what stealable() subsequently returns. If set to
135  /// true, indicates to the verbs that this buffer may be stolen from.
136  void setStealable(bool v) const { myStealable = v; }
137 
138  /// True if there are no VDB grids on GPU or CPU
139  bool isDirty() const { return !isOnGPU() && !isOnCPU(); }
140 
141  /// Frees all buffers and marks dirty.
142  void freeBuffers();
143 
144  /// Frees all the memory
145  void destroy();
146 
147  /// True if there are any CPU or GPU buffers, they may not have
148  /// valid data - use isDirty() to see that.
149  bool allocated() const { return myData ? true : false; }
150 
151  int64 getMemoryUsage() const;
152  int64 getDeviceMemoryUsage() const;
153 
155  { myProperties = props; }
157  { return myProperties; }
158 
159  /// Updates the contents of the properties, first making sure it is
160  /// unique. The provided operator should take a reference to
161  /// a UT_Options that it will update.
162  /// this->update([](UT_Options &opt) { opt.setOptionS("test", "bar"); });
163  template <typename OP>
164  void updateProperties(const OP &op)
165  { myProperties.update(op); }
166 
167  /// Returns the contiguous raw bytes for a numeric non-array attribute.
168  bool rawData(UT_WorkBuffer &data) const;
169  /// Replaces the contiguous raw bytes for a numeric non-array attribute.
170  bool setRawData(const char *data, exint length);
171 
172  /// Attribute registration to allow vdbs to be converted
173  /// to integers across HOM boundaries.
174 
175  /// Registers & returns the handle, adding the handle to the registered
176  /// list
177  static int registerAttribute(IMX_AttributeConstPtr vdb, UT_IntArray &registered);
178  /// Unregister all vdbs corresponding to the provided list,
179  /// erase the list afterwards. Assertion if handle wasn't regsitered
180  static void unregisterAttributes(UT_IntArray &registered);
181  /// Look up a registered vdb by handle.
182  static IMX_AttributeConstPtr lookupAttribute(int handle);
183 
184  /// This function calls operator() on op with a zero-initialized value
185  /// matching the attribute's storage type and number of channels.
186  template <typename OP>
187  void dispatch(const OP &op) const
188  {
189  int n = getChannels();
190  switch (GAgetDefaultStorage(storage(), precision()))
191  {
192  case GA_STORE_UINT8: dispatch<uint8>(op, n); break;
193  case GA_STORE_INT8: dispatch<int8>(op, n); break;
194  case GA_STORE_INT16: dispatch<int16>(op, n); break;
195  case GA_STORE_INT32: dispatch<int32>(op, n); break;
196  case GA_STORE_INT64: dispatch<int64>(op, n); break;
197  case GA_STORE_REAL16: dispatch<fpreal16>(op, n);break;
198  case GA_STORE_REAL32: dispatch<fpreal32>(op, n); break;
199  case GA_STORE_REAL64: dispatch<fpreal64>(op, n); break;
200  case GA_STORE_BOOL:
201  case GA_STORE_STRING:
202  case GA_STORE_DICT:
203  case GA_STORE_INVALID:
204  UT_ASSERT(!"Invalid storage type.");
205  break;
206  }
207  }
208 
209  /// This function calls operator() on op with a zero-initialized value
210  /// matching the storage type and number of channels.
211  template <typename T, typename OP>
212  static void dispatch(const OP &op, int channels)
213  {
214  switch (channels)
215  {
216  case 1: op((T) 0); break;
217  case 2: op(UT_Vector2T<T>(0, 0)); break;
218  case 3: op(UT_Vector3T<T>(0, 0, 0)); break;
219  case 4: op(UT_Vector4T<T>(0, 0, 0, 0)); break;
220  default:
221  UT_ASSERT(!"Unexpected number of channels.");
222  break;
223  }
224  }
225 
226  fpreal64 computeMin(int channel = 0) const;
227  fpreal64 computeMax(int channel = 0) const;
228  fpreal64 computeMinLength() const;
229  fpreal64 computeMaxLength() const;
230  fpreal64 computeAverage(int channel = 0) const;
231  exint computeMinI(int channel = 0) const;
232  exint computeMaxI(int channel = 0) const;
233 
234 protected:
237 
241 
242  /// Can data of this buffer be stolen by the verbs? TODO: who should reset
243  /// this and when?
244  mutable bool myStealable = true;
245 
246  friend IMX_API size_t
247  UTformatBuffer(char *buffer, size_t buffer_size, const IMX_Attribute &v);
248 };
249 
250 IMX_API size_t
251 UTformatBuffer(char *buffer, size_t buffer_size, const IMX_Attribute &v);
252 
253 #endif
254 
255 
IMX_Attribute & operator=(const IMX_Attribute &other)
Definition: IMX_Attribute.h:53
UT_SharedPtr< GA_CEAttribute > GA_CEAttributePtr
Definition: GA_Attribute.h:56
Definition of a geometry attribute.
Definition: GA_Attribute.h:203
Data has no numeric representation.
Definition: GA_Types.h:108
Class which stores the default values for a GA_Attribute.
Definition: GA_Defaults.h:32
void dispatch(const OP &op) const
GA_StorageClass
Definition: GA_Types.h:75
getFileOption("OpenEXR:storage") storage
Definition: HDK_Image.dox:276
const GLdouble * v
Definition: glcorearb.h:837
GA_Precision
Definition: GA_Types.h:91
bool isDirty() const
True if there are no VDB grids on GPU or CPU.
UT_OptionsHolder myProperties
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
Definition: SYS_Types.h:136
UT_OptionsHolder properties() const
GA_Defaults myDefaults
int64 exint
Definition: SYS_Types.h:125
void swap(T &lhs, T &rhs)
Definition: pugixml.cpp:7440
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:87
GA_TypeInfo typeInfo() const
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:39
void setOwner(GA_AttributeOwner owner)
int getChannels() const
3D Vector class.
4D Vector class.
Definition: UT_Vector4.h:176
2D Vector class.
Definition: UT_Vector2.h:162
GLuint buffer
Definition: glcorearb.h:660
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:243
void setStealable(bool v) const
bool allocated() const
bool stealable() const
double fpreal64
Definition: SYS_Types.h:201
UT_SharedPtr< GA_CEAttribute > myData
GLdouble n
Definition: glcorearb.h:2008
GA_CEAttributePtr data()
GA_StorageClass storage() const
void setTypeInfo(GA_TypeInfo typeinfo)
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
GA_AttributeOwner owner() const
UT_SharedPtr< const GA_CEAttribute > GA_CEAttributeConstPtr
Definition: GA_Attribute.h:57
GA_CEAttributeConstPtr data() const
IMX_Attribute(IMX_Attribute &&other) noexcept
Definition: IMX_Attribute.h:47
UT_SharedPtr< const IMX_Attribute > IMX_AttributeConstPtr
Definition: IMX_Attribute.h:36
long long int64
Definition: SYS_Types.h:116
static void dispatch(const OP &op, int channels)
UT_SharedPtr< IMX_Attribute > IMX_AttributePtr
Definition: IMX_Attribute.h:35
GA_CEAttributeAccessorPtr getCEAttributeAccessorRW()
Definition: IMX_Attribute.h:95
void updateProperties(const OP &op)
IMX_Attribute(const IMX_Attribute &other)
Definition: IMX_Attribute.h:46
GA_TypeInfo
Definition: GA_Types.h:105
UT_SharedPtr< GA_CEAttributeAccessor > GA_CEAttributeAccessorPtr
Definition: GA_Attribute.h:59
GLenum GLint GLint * precision
Definition: glcorearb.h:1925
void setDefaults(const GA_Defaults &def)
GA_AttributeOwner
Definition: GA_Types.h:35
IMX_Attribute & operator=(IMX_Attribute &&other)
Definition: IMX_Attribute.h:56
A map of string to various well defined value types.
Definition: UT_Options.h:87
const GA_Defaults & defaults() const
int tupleSize() const
bool isOnGPU() const
GA_CEAttributeAccessorPtr getCEAttributeAccessorW()
Definition: IMX_Attribute.h:93
#define IMX_API
Definition: IMX_API.h:8
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
GA_CEAttributeAccessorPtr getCEAttributeAccessorR() const
Definition: IMX_Attribute.h:91
IMX_API size_t UTformatBuffer(char *buffer, size_t buffer_size, const IMX_Attribute &v)
GA_Size entries() const
Definition: IMX_Attribute.h:99
bool isOnCPU() const
void setProperties(const UT_OptionsHolder &props)
Definition: format.h:1821
bool isArray() const
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)
GA_Precision precision() const
GLenum GLuint GLsizei const GLenum * props
Definition: glcorearb.h:2525
GLenum src
Definition: glcorearb.h:1793