HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RE_VertexArray.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: RE_VertexArray.h ( RE Library, C++)
7  *
8  * COMMENTS:
9  * These classes provide a GL-indepedent way of specify vertex attributes
10  * in array or constant form. Array attributes use VBOs, whereas constant
11  * attributes use glVertexAttrib*() calls.
12  */
13 #ifndef RE_VERTEX_ARRAY_H
14 #define RE_VERTEX_ARRAY_H
15 
16 class RE_Render;
17 class RE_ElementArray;
18 class RE_OGLConstAttribute;
19 
20 #include "RE_CachedObject.h"
21 #include "RE_Types.h"
22 #include "RE_Texture.h"
23 #include "RE_OGLBuffer.h"
24 #include <UT/UT_String.h>
25 #include <UT/UT_WorkBuffer.h>
26 #include <iosfwd>
27 
28 // Base class for various array types.
30 {
31 public:
32  // Returns a new vertex array
33  static RE_VertexArray *newAttribArray(const char *name, int num_elements=0,
34  const char *cachename = NULL,
36 
37  static RE_VertexArray *newElementArray(int num_elements=0,
38  const char *cachename = NULL);
39 
40  // Creates a container RE_VertexArray around the passed array.
41  static RE_VertexArray *newArrayContainer(RE_OGLBufferHandle &array,
42  const char *cachename = NULL,
44 
45  static RE_VertexArray *newArrayContainer(UT_Array<RE_OGLBufferHandle>&array,
46  const char *cachename = NULL,
48 
49  // Returns the cache name
50  static void getAttribCacheName(UT_WorkBuffer &str,
51  const char *name,
52  RE_ArrayType attrib_type,
53  const char *cachename);
54 
55  ~RE_VertexArray();
56 
57  // Creates a new RE_VertexArray with exactly the same format and
58  // buffer references.
59  RE_VertexArray *clone() const;
60 
61  /// Returns the amount of main memory (NOT graphics memory!)
62  /// owned by this RE_VertexArray.
63  int64 getMemoryUsage(bool inclusive) const;
64 
65  // add this buffer to the GL cache using a tag of 'name'/attribname
66  void cacheBuffer(const char *name);
67  const char *getCacheName() const { return myCacheName; }
68 
69  // Set the version number on the cached buffer.
70  void setCacheVersion(RE_CacheVersion v);
71  RE_CacheVersion getCacheVersion() const;
72 
73  // Indicate if this array is actively in use by the cache.
74  void markAsUsed();
75  void markAsUnused(bool move_to_unused_cache = true);
76  bool isUsed() const { return myInUse; }
77 
78  // sets the cache tag on underlying arrays. The cache tag is used to notify
79  // other entities when the array is evicted from the cache.
80  void setCacheTag(RE_CacheTagHandle h);
81 
82  /// Hint to the GL driver how this buffer will be used. May only be set
83  /// before the buffer is created (initialize(), setArray(), etc).
84  void setUsage(RE_BufferUsageHint h);
85 
86  /// Set the buffer to use persistent mapped buffers, optionally with
87  /// coherent behaviour (writes seen automatically). RE_EXT_BUFFER_STORAGE
88  /// must be supported, or false will be returned and the setting will have
89  /// no effect.
90  void setPersistent(RE_PersistentBufferMode mode);
91 
92  // assigns 'num' buffers to this vertex array, cachable with 'cachename'.
93  void adoptArrays(const UT_Array<RE_OGLBufferHandle> &buffers,
94  const char *cachename);
95 
96  // Removes all contained arrays, possibly deleting them if not cached.
97  void resetBuffer(bool delete_cached = false,
98  bool mark_as_unused = true);
99 
100  // If the size, type or format of the buffer is changed, this method can
101  // ensure that the underlying array is initialized afterward. 'created'
102  // can be used to query if a new buffer was allocated by this method.
103  bool initialize(RE_Render *r, bool *created = NULL);
104 
105  // Allows some extra data to be cached with the buffer. Caching must be
106  // enabled, and this can't be a constant attribute (no buffer).
107  // Owndership of the data is always given up, and it will be deleted when
108  // the buffer is.
109  bool setExtraBufferData(RE_CachedExtraData *data);
110  RE_CachedExtraDataHandle getExtraBufferData() const;
111 
112  // Setup
113  // standard GL attributes use the buffer types
114  void setBufferType(RE_BufferType type);
115 
116  // custom attributes are referenced by name.
117  void setAttributeName(const char *attrib);
118 
119  // Sets the data type for the array. This type is one array 'element'.
120  // If normalized is true, fixed formats will be normalized to [0,1]
121  void setFormat(RE_GPUType type, int vectorsize,
122  bool normalized = true);
123 
124  // Sets the length of the array, in elements (a fp32 vec3 is 1 element)
125  void setLength(int len);
126 
127  // If non-zero, this attribute advances once every 'nprims' number of
128  // primtives instead of once per vertex. RE_EXT_INSTANCED_ARRAYS required.
129  void setInstanceStride(int nprims);
130 
131  // if non-negative, specifies an instance draw group to which this belongs
132  void setInstanceGroup(int instance_group)
133  { myInstanceGroup = instance_group; }
134 
135  // Sets the sampling frequency of this array - either per point, vertex,
136  // primitive, instance or randomly accessed.
137  void setArrayType(RE_ArrayType t) { myArrayType = t; }
138 
139  // Array data methods
140 
141  // Specifies all the data at once, or a subrange of the data if sublen!=0.
142  // offset and sublen are specified in elements, not bytes.
143  bool setArray(RE_Render *r,
144  const void *data,
145  int offset = 0,
146  int sublen = 0,
147  int array_index = 0);
148 
150  const void *data,
151  int array_index = 0)
152  { return setArray(r,data, 0,myLength, array_index); }
153 
155  const void *data,
156  int offset,
157  int length,
158  int array_index)
159  { return setArray(r, data, offset, length, array_index); }
160 
161 
162  // fetch data from a buffer, starting at element 'offset' for 'sublen'
163  // elements (the size of an element depends on the format & type). If
164  // sublen is zero, it will fetch to the end of the buffer.
165  bool getArray(RE_Render *r,
166  void *data,
167  int offset = 0,
168  int sublen = 0,
169  int array_index = 0) const;
170 
171  // Cuts loose the current buffer and recreates a new one. Will still
172  // maintain the same buffer ID, but the underlying chunk of memory will be
173  // handed off to the driver.
174  void orphan(RE_Render *r);
175 
176  // The attribute is constant-valued. Data must be big enough to fill one
177  // element, and the data is always FP. Constant values cannot be mapped.
178  void setConstant(const fpreal32 *data);
179  void setConstant(const fpreal64 *data);
180 
181  // Ensure that a constant is created without assigning any data.
182  void createConstant();
183 
184  // Internally constant data is stored as double.
185  fpreal64 *getConstant();
186 
187  // Indicate that the vertex array is a default value placeholder.
188  void markAsDefaultValue(bool def) { myDefaultValue = def; }
189  bool isDefaultValue() const { return myDefaultValue; }
190 
191  // Returns a pointer to the array so it can be modified. must call unmap()
192  // when finished, before attempting to use the array again. This maps the
193  // entire buffer, so in some instances it may be better to use setArray()
194  // with a subrange.
195  // NOTE: map()/unmap() is generally slower than setArray(), so use only
196  // for reading or persistent buffers.
197  void *map(RE_Render *r,
199  int array_index = 0);
200  void *mapRange(RE_Render *r, int start, int length,
202  int array_index = 0);
203  void unmap(RE_Render *r, int array_index = 0);
204 
205  // For persistent mapped buffers, this returns the currently mapped data.
206  // Will only return a value if mapped, persisent is set, and
207  // RE_EXT_BUFFER_STORAGE is supported.
208  void *getPersistentMap(int array_index = 0) const;
209 
210  // Returns true if this is a persistently mapped buffer
211  bool isPersistentMap() const;
212 
213  // Zero the buffer.
214  void zero(RE_Render *r);
215 
216  // Attach a texture buffer object to a VERTEX, PRIM or RANDOM array type.
217  bool attachTexBuffer(RE_Render *r,
218  RE_Texture *tex);
219 
220  // Return the TBO attached to a VERTEX, PRIM or RANDOM array type.
221  RE_Texture *getTextureBufferObject() const; // inlined below
222 
223  // Binding methods
224 
225  // Binds the array to the given array attribute type for subsequent draw
226  // calls. When drawing with multiple attributes, the first N-1 should be
227  // bound, and the Nth should call draw...Prims().
228  // geo_obj is only true when called from RE_Geometry.
229  bool bind(RE_Render *r, unsigned int stride = 0,
230  bool geo_obj=false,
231  const char *shader_attrib = NULL);
232  bool unbind(RE_Render *r);
233 
234  // Rebinds an array to a new attribute location due to a shader change.
235  bool rebindAttrib(RE_Render *r, int location);
236  bool unbindAttrib(RE_Render *r);
237 
238  // Bind to a VAO.
239  bool setupForAttribIndex(RE_Render *r, int attrib_location,
240  RE_GPUType t, int stride);
241 
242 
243  // These bind methods bind the buffer to the specificied buffer binding
244  // point, which may be different than the buffer type's default binding
245  // point. Some binding points are indexed (uniform and transform feedback)
246  // while most have a single binding point.
247  bool bindBuffer(RE_Render *r, RE_BufferBinding point, int index=0,
248  int array_index=0);
249  bool unbindBuffer(RE_Render *r, RE_BufferBinding point, int index=0,
250  int array_index=0);
251 
252  // Drawing methods
253 
254  // Draws primitives using every n'th point (where n=stride) in this array
255  // as 'prim' primitive type.
256  void drawPrims(RE_Render *r,
257  RE_PrimType prim,
258  unsigned int stride,
259  int num_instances,
260  bool enable_arrays,
261  int vertices_per_patch);
262 
263  // Draws primitives using a subrange of points in this array
264  void drawSomePrims(RE_Render *r,
265  RE_PrimType prim,
266  int start,
267  int num,
268  int num_instances,
269  bool enable_arrays,
270  int vertices_per_patch);
271 
272  // Draws primitives using some points in this array using a 2nd array as
273  // the indices. The array must be an element array.
274  void drawSomePrims(RE_Render *r,
275  RE_PrimType prim,
276  RE_VertexArray *index_array,
277  int index_offset,
278  int index_num_elements,
279  int num_instances,
280  bool enable_arrays,
281  int vertices_per_patch);
282 
283  // Query
284  RE_BufferType getBufferType() const { return myBufferType; }
285  RE_GPUType getDataType() const { return myType; }
286  int getVectorSize() const { return myVectorSize; }
287  int getLength() const { return myLength; }
288  int getNumBuffers() const { return myArray.entries(); }
289  const RE_OGLBufferHandle &getArrayBuffer(int i=0) const
290  { return (i < myArray.entries()) ? myArray(i) : theNullArray; }
291  const char *getAttributeName() const { return myAttribName; }
292  RE_ArrayType getArrayType() const { return myArrayType; }
293  bool isBufferBound(int array_index=0) const;
294  bool isConstantBuffer() const { return myConstantFlag; }
295  void getHoudiniName(UT_String &name) const;
296  void getGLName(UT_String &name) const;
297  int getInstanceStride() const { return myInstanceStride; }
298  int getInstanceGroup() const { return myInstanceGroup; }
299 
300  int64 getSizeBytes() const;
301 
302  // Debugging prints
303  void print(std::ostream *os = nullptr) const;
304  void printValues(RE_Render *r, std::ostream &os,
305  int start=0, int len=0) const;
306 
307  void addRef(void (*callback)(void *), void *data);
308  void removeRef(void (*callback)(void *), void *data);
309 
310  // Some attributes are 'known' types, as listed in RE_GenericAttribID.
311  // Setting the ID can help performance. Do not change the type of an attrib
312  // while bound to an RE_Geometry; this should only be called once.
314  { UT_ASSERT(myGenID == RE_GENATTRIB_UNDEF || myGenID == id);
315  myGenID = id; }
316  RE_GenericAttribID getGenericID() const { return myGenID; }
317 
318 protected:
319  RE_VertexArray(RE_BufferType type, int nelements = 0);
320  RE_VertexArray(const char *name, int nelements = 0);
321  RE_VertexArray(const RE_VertexArray &va);
322 
323  void detachTexBuffer(int index);
324  void generateCacheName(UT_String &cachename,
325  int index) const;
326 
332  int myLength;
337  RE_OGLConstAttribute *myConstAttrib;
340  bool myInUse;
341 
342  bool myMappedFlag[4];
351 
353  {
354  public:
355  re_VertexArrayCB(void (*cb)(void *),void *d) : callback(cb), data(d) {}
356  void (*callback)(void *);
357  void *data;
358  };
359 
361 };
362 
363 inline RE_Texture *
365 {
366  return (myArray.entries() == 1) ? myArray(0)->getTBO() : NULL;
367 }
368 
369 /// Shallow reference to an RE_VertexArray. Will be cleared if the
370 /// RE_VertexArray is deleted.
372 {
373 public:
374  RE_VertexArrayRef() : myArray(NULL) {}
376 
377  bool isValid() { return myArray!=NULL; }
378  void operator=(RE_VertexArray &va);
379  void clear();
380  RE_VertexArray *get() { return myArray; }
382  {
383  UT_ASSERT(myArray);
384  return myArray;
385  }
386 
387 private:
388  static void arrayDeleted(void *);
389 
390  RE_VertexArray *myArray;
391 };
392 
393 #endif
bool replaceArrayRange(RE_Render *r, const void *data, int offset, int length, int array_index)
RE_GenericAttribID getGenericID() const
int getNumBuffers() const
#define RE_API
Definition: RE_API.h:10
void setArrayType(RE_ArrayType t)
int getLength() const
const GLdouble * v
Definition: glcorearb.h:836
GLuint start
Definition: glcorearb.h:474
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
UT_Array< re_VertexArrayCB > myRefs
bool isDefaultValue() const
RE_PersistentBufferMode myPersistentBufferMode
RE_ArrayType myArrayType
RE_GenericAttribID myGenID
png_uint_32 i
Definition: png.h:2877
RE_OGLConstAttribute * myConstAttrib
GLuint id
Definition: glcorearb.h:654
const char * getAttributeName() const
RE_GenericAttribID
Definition: RE_Types.h:321
long long int64
Definition: SYS_Types.h:106
RE_GPUType
Definition: RE_Types.h:42
RE_BufferType
Definition: RE_Types.h:262
RE_BufferType myBufferType
bool isConstantBuffer() const
RE_BufferBinding
Definition: RE_Types.h:298
RE_CacheTagHandle myCacheTagHandle
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
RE_GPUType getDataType() const
UT_String myCacheName
GLuint GLint GLboolean GLint GLenum access
Definition: glcorearb.h:2221
void setGenericID(RE_GenericAttribID id)
double fpreal64
Definition: SYS_Types.h:191
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:871
GLintptr offset
Definition: glcorearb.h:664
Wrapper around hboost::intrusive_ptr.
GLint location
Definition: glcorearb.h:804
GLboolean * data
Definition: glcorearb.h:130
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:316
GLuint const GLchar * name
Definition: glcorearb.h:785
GLint GLenum GLboolean normalized
Definition: glcorearb.h:871
bool replaceArray(RE_Render *r, const void *data, int array_index=0)
int getInstanceStride() const
bool myPersistentMapCopied
GLenum mode
Definition: glcorearb.h:98
const GLuint * buffers
Definition: glcorearb.h:660
RE_BufferUsageHint myUsage
void markAsDefaultValue(bool def)
static RE_OGLBufferHandle theNullArray
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
re_VertexArrayCB(void(*cb)(void *), void *d)
RE_BufferAccess
Definition: RE_Types.h:291
bool isUsed() const
GLuint index
Definition: glcorearb.h:785
const RE_OGLBufferHandle & getArrayBuffer(int i=0) const
RE_VertexArray * operator->()
RE_ArrayType
Definition: RE_Types.h:310
const char * getCacheName() const
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
RE_ArrayType getArrayType() const
RE_Texture * getTextureBufferObject() const
RE_BufferUsageHint
Definition: RE_Types.h:273
png_infop png_uint_32 int num
Definition: png.h:2158
Simple class for a mutli-integer cache tag.
RE_GPUType myType
GLboolean r
Definition: glcorearb.h:1221
UT_Array< RE_OGLBufferHandle > myArray
UT_String myAttribName
int getInstanceGroup() const
int getVectorSize() const
RE_BufferType getBufferType() const
RE_PrimType
Definition: RE_Types.h:191
float fpreal32
Definition: SYS_Types.h:190
RE_PersistentBufferMode
Definition: RE_Types.h:501
void setInstanceGroup(int instance_group)
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:794