HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RE_Geometry.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_Geometry.h ( RE Library, C++)
7  *
8  * COMMENTS:
9  *
10  * This class acts as a wrapper around multiple RE_VertexArray objects,
11  * similar to the way a framebuffer object contains multiple textures.
12  * Arrays can be attached to various attachments points, representing
13  * different vertex attribute data
14  *
15  */
16 #ifndef RE_GEOMETRY_H
17 #define RE_GEOMETRY_H
18 
19 #include "RE_Types.h"
20 
21 class RE_ElementArray;
22 class RE_Render;
23 class RE_Shader;
24 class RE_VertexState;
25 class RE_VertexArray;
26 class RE_VertexMap;
27 class re_Attrib;
28 class re_Connectivity;
29 class re_InstanceGroup;
30 
31 #include <UT/UT_ValArray.h>
32 #include <UT/UT_String.h>
33 #include <UT/UT_StringArray.h>
34 #include <UT/UT_SymbolTable.h>
35 #include <iosfwd>
36 
37 #include "RE_API.h"
38 #include "RE_Types.h"
39 #include "RE_Texture.h"
40 #include "RE_CachedObject.h"
41 
42 #define RE_GEO_POINTS_IDX 0
43 #define RE_GEO_WIRE_IDX 2
44 #define RE_GEO_SHADED_IDX 4
45 
46 #define RE_GEO_ALL_SHADED 0x40000000
47 
48 /// @brief A collection of vertex arrays defining a geometry object.
49 /// This class acts as a wrapper around multiple RE_VertexArray objects,
50 /// similar to the way a framebuffer object contains multiple textures.
51 /// Arrays can be attached to various attachments points, representing
52 /// different vertex attribute data
54 {
55 public:
56  RE_Geometry(int num_points = 0,
57  bool use_buffer_object = true);
58  ~RE_Geometry();
59 
60  /// Returns the amount of main memory (NOT graphics memory!)
61  /// owned by this RE_Geometry.
62  int64 getMemoryUsage(bool inclusive) const;
63 
64  /// @brief Choose between buffer objects and client arrays
65  /// By default, buffer objects are used if supported. This allows you to
66  /// turn them off and use vertex arrays. This is useful if the geometry
67  /// information is throw-away. If called when vertex arrays are attached,
68  /// they will be cleared (and possibly deleted)
69  void useBufferObjects(bool use_buf = true);
70 
71  /// @brief Optimize this geometry with vertex array objects.
72  /// Use an RE_VertexState (and GL Vertex Array Objects) to contain vertex
73  /// arrays. Requires RE_EXT_VERTEX_ARRAY_OBJECT.
74  void useVertexState(bool use_state = true);
75 
76  /// Clears all the attached buffers, and removes them from the cache.
77  void purgeBuffers();
78 
79  /// @brief Number of points in the geometry.
80  /// Number of points for the arrays declared as RE_ARRAY_POINT. This will
81  /// clear the data in all point arrays and reset the connectivty.
82  //@{
83  bool setNumPoints(int num);
84  int getNumPoints() const { return myNumPoints; }
85  //@}
86 
87  /// Sets the number of elements in arrays declared as RE_ARRAY_VERTEX.
88  //@{
89  bool setNumVertices(int num);
90  int getNumVertices() const { return myNumVertices; }
91  //@}
92 
93  /// Sets the number of elements in arrays declared as RE_ARRAY_PRIMITIVE.
94  //@{
95  bool setNumPrimitives(int num);
96  int getNumPrimitives() const { return myNumPrimitives; }
97  //@}
98 
99  /// @brief Calulcate the number of points required for some primitives
100  /// This will ensure that the buffer is big enough to hold enough vertices
101  /// for the given number of primitives of the specified type. The buffer
102  /// size can be reduced if 'shrink_if_smaller' is true, otherwise this will
103  /// only increase the size of the buffer if needed. If 'shader' is given,
104  /// it will check if the shader has a geometry shader and modify the
105  /// number of vertices by the output primitive type and maximum #vertices.
106  /// Shaders without a geometry shader will have no effect (as if NULL).
107  /// For transform feedback, only POINTS, LINES or TRIANGLES are ever
108  /// produced.
109  void resizePointsToFit(RE_PrimType primtype,
110  int num_primitives,
111  bool shrink_if_smaller,
112  RE_Shader *shader = NULL);
113 
114  /// @brief Create buffers for all attached arrays
115  /// When the above methods are called with attached arrays, these arrays
116  /// might have their storage freed. This call will ensure that all arrays
117  /// are initialized.
118  void initialize(RE_Render *r);
119 
120  // ---------------------------------------------------------------------
121  /// @name Generic Vertex Attributes
122  /// GL3 shaders use generic vertex attributes, which are identified by name.
123  /// GL2 shaders can use a few generic vertex attributes along with the fixed
124  /// builtin attributes. Generic attributes must be used for instanced,
125  /// primitive or vertex atttributes.
126  //@{
127 
128  /// place all attributes in a stashed list, which clears them externally
129  /// but keeps them available internally. If createAttribute() or
130  /// findCached...() are called, attributes will be fetched from the stashed
131  /// list. This should not be called without a clearStashedAttributes()
132  /// pairing it, nor called twice in a row.
133  void stashAttributes();
134 
135  /// Remove all remaining stashed attributes. If purge is true, remove them
136  /// from the GL cache as well.
137  int clearStashedAttributes(bool purge_from_cache);
138 
139  /// fetch stashed attribute 'name', placing it back into the geometry's list
140  RE_VertexArray *recallStashedAttribute(const char *name);
141 
142  /// fetch stashed attribute 'name', but only if it is a const buffer.
143  RE_VertexArray *recallStashedVaryingAttribute(const char *attrib_name);
144 
145  /// fetch stashed attribute 'name', but only if it is a const buffer.
146  RE_VertexArray *recallStashedConstAttribute(const char *attrib_name);
147 
148  /// fetch stashed attribute 'name' from the stash list, removing it from
149  /// the list and returning it if found. This does not place it back in the
150  /// geometry's list of attributes.
151  RE_VertexArray *fetchStashedAttribute(const char *name);
152 
153  bool hasStashedAttribute(const char *name);
154 
155 
156  /// @brief Create a generic vertex attribute attached to 'attrib_name'
157  /// Attributes are referenced by name, rather than buffer type. Array size
158  /// is required if array type is RANDOM or INSTANCED.
159  RE_VertexArray *createAttribute(RE_Render *r,
160  const char *attrib_name,
161  RE_GPUType data_format,
162  int vectorsize,
163  const void *data,
165  int array_size = 0,
167  const char *cache_prefix = NULL);
168 
169  /// @brief Create an instanced vertex attribute attached to 'attrib_name'
170  /// Create an attribute which advances once every 'instance_stride'
171  /// instances rather than once per vertex. RE_EXT_INSTANCED_ARRAYS is
172  /// required, and an instanced drawing method must be used (or it acts as a
173  /// uniform).
174  RE_VertexArray *createInstancedAttribute(RE_Render *r,
175  const char *attrib_name,
176  RE_GPUType data_format,
177  int vectorsize,
178  int instance_stride,
179  int num_instances,
180  const void *data,
181  const char *cache_prefix=NULL);
182  /// @brief Create a constant attribute value
183  /// Only RE_GPU_FLOAT32 and _FLOAT64 are supported for constant data.
184  /// The 'data' point must hold at least 1 element (sizeof(type)*vectorsize).
185  RE_VertexArray *createConstAttribute(RE_Render *r,
186  const char *attrib_name,
187  RE_GPUType data_format,
188  int vectorsize,
189  const void *data);
190 
191  /// @brief Create a constant attribute if varying attribute is not found
192  /// Ensures that there is at least a constant attribute value available if
193  /// the attribute doesn't exist already. Only returns an array if one was
194  /// created.
195  RE_VertexArray *assignAttributeDefault(RE_Render *r,
196  const char *attrib_name,
197  RE_GPUType data_format,
198  int vectorsize,
199  const void *default_value);
200 
201  /// Attach an existing attribute using its name as the binding
202  bool attachAttribute(RE_VertexArray *attrib);
203  /// Detatch an attribute from this object by name
204  RE_VertexArray *detachAttribute(const char *name);
205 
206  /// @brief Delete an attached vertex attribute by name
207  /// delete the RE_VertexArray, and if purge_cache is true, remove the
208  /// underlying data buffer from the GL cache.
209  bool clearAttribute(const char *name,
210  bool purge_cache=false);
211 
212  /// @brief Delete an attached vertex attribute by index
213  /// delete the RE_VertexArray, and if purge_cache is true, remove the
214  /// underlying data buffer from the GL cache.
215  bool clearAttributeByIndex(int i,
216  bool purge_cache=false);
217 
218  /// Fetch an attribute by name
219  RE_VertexArray *getAttribute(const char *name) const;
220 
221  /// Return an attribute's index byattribute name. Can change if attributes
222  /// are removed or added.
223  int getAttributeIndex(const char *name) const;
224 
225  /// Fetch an attribute by known type
226  RE_VertexArray *getAttribute(RE_GenericAttribID attrib_id) const;
227 
228  /// Return the number of generic attributes currently attached.
229  int getNumAttributes() const;
230 
231  /// Return currently attached vertex attribute 'i'
232  RE_VertexArray *getAttributeByIndex(int i) const;
233 
234  /// Return a vertex map representing the layout locations of the attributes
235  const RE_VertexMap *getVertexMap() const { return myVertexMap; }
236 
237  //@}
238 
239  // ---------------------------------------------------------------------
240  /// @name Addressable attributes
241  /// Addressable attributes allow you to use buffers, but sample them like
242  /// textures using texelFetch on samplerBuffer uniforms. Only buffer objects
243  /// are supported.
244  //@{
245 
246  /// @brief Create a randomly addressable attribute (texture buffer object)
247  /// Attributes can be stored in texture buffers, and accessed out of
248  /// the normal vertex flow, such as for vertex or primitive attributes.
249  /// Unlike normal attributes, addressable attributes can have a different
250  /// array size than the RE_Geometry's number of vertices.
251  RE_VertexArray *createAddressableAttribute(RE_Render *r,
252  const char *attrib_name,
253  int length,
254  RE_GPUType data_format,
255  int vectorsize,
256  const void *data,
257  RE_ArrayType atype
258  = RE_ARRAY_RANDOM,
259  const char *cache_prefix=NULL);
260  /// Create a randomly addressable attribute from an existing vertex array
261  RE_Texture *createAddressableAttribute(RE_Render *r,
262  const char *attrib_name,
264 
265  /// Fetch an addressable attribute by name
266  RE_VertexArray *getAddressableAttribute(const char *attrib_name) const;
267 
268  /// Return the index of the named addressable attribute, or -1 if not found
269  int getAddressableAttributeIndex(const char *attrib_name)
270  const;
271 
272  /// Delete the addressable attribute named 'attrib_name'
273  void clearAddressableAttribute(const char *attrib_name);
274 
275  /// Return the number of addressable attributes
277  { return myTextureAttributes.entries(); }
278 
279  /// Return the texture buffer object representing addressable attribute 'i'
281  { return myBufferTextures(index); }
282  /// Return the vertex arrayrepresenting addressable attribute 'i'
284  { return myTextureAttributes(index); }
285 
286  //@}
287 
288  // ---------------------------------------------------------------------
289  /// Caching @n
290 
291  /// Use caching for all vertex and element arrays stored by this object.
292  /// If any vertex arrays are attached, they are cleared unless the name
293  /// matches the previous cache name.
294  //@{
295 
296  /// @brief Set the base cache name.
297  /// Attribute names will be concatenated to the base name to form the full
298  /// cache name. Passing NULL will disable caching. Returns true if the
299  /// cachename changed, in which case all existing buffers will be purged
300  /// from the cache.
301  bool cacheBuffers(const char *name);
302 
303  /// Return the base cache name.
304  const char *getCacheName() const { return myCacheName; }
305 
306  /// Check if this geometry object is caching.
307  bool isCaching() const { return myCacheName.isstring(); }
308 
309  /// Remove all buffers from the cache when this geometry object is deleted.
310  void purgeOnDelete(bool purge = true)
311  { myPurgeOnDelete = purge; }
312 
313  /// set the cache version for all arrays attached to this geometry object.
314  void setCacheVersion(RE_CacheVersion v);
315 
316  /// @brief Assign a cache tag to monitor cache changes on this object
317  /// The tag will be bumped if this or any of its vertex buffers are removed
318  /// from the cache.
319  void setCacheTag(RE_CacheTagHandle h);
320 
321  /// @brief Find an attribute or array in the GL cache, possibly creating it
322  /// Returns the cached array, if it exists, for the array named
323  /// 'attrib_name'. The name may be one of the gl-builtin attributes, like
324  /// gl_Position or gl_Color. 'array_size' is the size of an INSTANCE or
325  /// RANDOM array_type.
326  RE_VertexArray *findCachedAttrib(RE_Render *r,
327  const char *attrib_name,
328  RE_GPUType data_format,
329  int vectorsize,
330  RE_ArrayType array_type,
331  bool create_if_missing = false,
332  int random_array_size = -1,
333  const RE_CacheVersion *cv=NULL,
335  const char *cache_prefix = NULL);
336 
337  /// @brief Find an instanced attribute in the GL cache, possibly creating
338  /// it. The instance_step parameter defines how often the attribute
339  /// is advanced when the instance ID changes - 1 is once per instance.
340  RE_VertexArray *findCachedInstancedAttrib(RE_Render *r,
341  const char *attrib_name,
342  RE_GPUType data_format,
343  int vectorsize,
344  int instance_step,
345  int array_size,
346  bool create_if_missing=false,
347  const RE_CacheVersion *v=NULL,
349  const char *cache_prefix=NULL);
350 
351 
352  //@}
353 
354  // ---------------------------------------------------------------------
355  /// @name Instance groups
356  /// Instance groups allow for rendering of the geometry with different
357  /// attributes, generally InstanceTransform, but others can be overriden
358  /// as well. The base (default) group is instance group 0. Higher groups
359  /// will substitute their attributes for the ones in instance group 0. Note
360  /// that the attribute need not exist in the base group to be overriden.
361  /// If an attribute is not overriden, the base group attribute is used.
362 
363  /// Create a new instance group. instance_group must be a positive int.
364  void createInstanceGroup(int instance_group);
365 
366  /// Remove an existing instance group. Will not remove any attached
367  /// attribute to that group, but the instance group can no longer be
368  /// rendered.
369  void removeInstanceGroup(int instance_group);
370 
371  /// Returns true if the specified instance group exists.
372  bool hasInstanceGroup(int instance_group) const;
373 
375  { return myInstanceGroups.entries(); }
376 
377  /// For instanced drawing, create indirection list to render only some of
378  /// the instances. The base instance group can also be indexed.
379  void setInstanceGroupIndexList(RE_Render *r,
380  int instance_group,
381  bool trivial,
382  const UT_IntArray *indices=NULL);
383 
384  /// Set to draw a single instance in the group.
385  void setInstanceGroupConstIndex(RE_Render *r,
386  int instance_group,
387  int instance_to_draw);
388 
389  /// Set to draw all instances (based on the # transforms).
390  void setInstanceGroupDrawEverything(RE_Render *r,
391  int instance_group);
392  /// Draw none of the instances.
393  void setInstanceGroupDrawNothing(RE_Render *r,
394  int instance_group);
395 
396 
397  /// Create a constant for an attribute in 'instance_group'.
398  RE_VertexArray *createConstInstanceGroupAttrib(RE_Render *r,
399  int instance_group,
400  const char *name,
401  RE_GPUType data_format,
402  int vectorsize,
403  const void *data);
404 
405  /// Create a constant transform for an instance group.
406  void setConstInstanceGroupTransform(int instance_group,
407  const UT_Matrix4D &xform,
408  bool remove_instanced_xform);
409  void setConstInstanceGroupTransform(int instance_group,
410  const UT_Matrix4F &xform,
411  bool remove_instanced_xform);
412 
413 
414  /// @brief Find an instance-group attribute in the cache
415  /// Creates an instanced attribute for an instance-group, which overrides
416  /// the base attribute on the RE_Geometry. instance_group zero is the base
417  /// level ("Cd"), all other groups have the group appended ("Cd1" for
418  /// group 1). When drawn with drawInstanceGroup(), attributes in the
419  /// specified instance group will be used (if present) instead of the base
420  /// attributes.
421  RE_VertexArray *findCachedInstanceGroupAttrib(RE_Render *r,
422  int instance_group,
423  const char *name,
424  RE_GPUType data_type,
425  int vector_size,
426  int instance_step,
427  int num_instances,
428  bool create=false,
429  const RE_CacheVersion *v=NULL,
432  const char *view_name = NULL);
433 
434  /// Return the attribute in the instance group with type 'attrib_id', or
435  /// if it's not a known type, by name.
436  RE_VertexArray *getInstanceGroupAttrib(int instance_group,
437  RE_GenericAttribID attrib_id,
438  const char *name);
439  /// Remove and delete the array.
440  bool clearInstanceGroupAttrib(int instance_group,
441  RE_GenericAttribID attrib_id,
442  const char *name);
443 
444  /// This is the same as getInstanceGroupAttrib(), but it used mainly by
445  /// RE_Shader to do additional prep on the attrib.
446  RE_VertexArray *getInstanceGroupTextureBufferForShader(RE_Render *r,
447  RE_Shader *sh,
448  int instance_group,
449  RE_GenericAttribID attr_id,
450  const char *name);
451 
452  /// Returns the number of instances to be drawn in an instance group.
453  int getInstanceGroupCount(int instance_group) const;
454 
455  /// Return a full instance group name based on an original base attrib name
456  static void getInstanceGroupName(UT_WorkBuffer &inst_name,
457  const char *base_name,
458  int instance_group);
459 
460  /// Return a full instance group name based on the known attribute type
461  static void getInstanceGroupName(UT_WorkBuffer &inst_name,
462  RE_GenericAttribID attrib_type,
463  int instance_group);
464 
465  // ---------------------------------------------------------------------
466  /// Primitive Connectivity @n
467 
468  /// These methods allow you to set up and cache primitive connectivity.
469  /// Each bit of primitive connectivity is added to a named group, like
470  /// 'wireframe' or 'shaded'. A connectivity group can have multiple
471  /// types of primitives or materials. Groups are indexed, using int ids.
472  /// You can pass an optional material to each set of connected prims. If
473  /// the material is null, it will use the last used material (which may be
474  /// the material of earlier connected primitives). Materials are not owned
475  /// by this class; the element arrays are. If 'replace' is true, any
476  /// previous connectivity established with the name 'connect_group'
477  /// will be removed and replaced with the new connectivity.
478  //@{
479 
480  /// Connect all primtives and place them in indexed group 'connect_index'
481  int connectAllPrims(RE_Render *r,
482  int connect_index,
483  RE_PrimType prim,
484  const RE_MaterialPtr &mat = NULL,
485  bool replace = false,
486  int vertices_per_patch = 0);
487 
488  /// Connect a subrange of vertices and place in index group 'connect_index'
489  int connectSomePrims(RE_Render *r,
490  int connect_group,
491  RE_PrimType prim,
492  int start,
493  int length,
494  unsigned int stride = 0,
495  const RE_MaterialPtr &mat = NULL,
496  bool replace = false,
497  int vertices_per_patch = 0);
498 
499  /// @brief Connect vertices using an indexed vertex list, add to 'connect_group'.
500  /// 'num' is the size of the list, not the number of primitives. If
501  /// any index is greater than the number of points in the geometry,
502  /// the results will be undefined. This is added to connect_group.
503  int connectIndexedPrims(RE_Render *r,
504  int connect_group,
505  RE_PrimType prim,
506  int num,
507  const unsigned int *prims,
508  const RE_MaterialPtr &mat = NULL,
509  bool replace = false,
510  int vertices_per_patch = 0);
511 
512  /// Connect vertices using the indices in the buffer object 'elements'
513  int connectIndexedPrims(RE_Render *r,
514  int connect_group,
515  RE_PrimType prim,
516  RE_VertexArray *elements,
517  const RE_MaterialPtr &mat = NULL,
518  bool replace = false,
519  int vertices_per_patch = 0);
520 
521  /// Connect vertices using the indices in the element array 'elements'
522  int connectIndexedPrims(RE_Render *r,
523  int connect_group,
524  RE_ElementArray *elements,
525  const RE_MaterialPtr &mat = NULL,
526  bool replace = false);
527 
528 
529  /// Returns the number of discrete connectivities in 'connect_group'
530  int getConnectNumElementArrays(int connect_group);
531  /// Returns the element array associated with connect_group, if any.
532  RE_ElementArray *getConnectElementArray(int connect_group,
533  int index = 0);
534 
535  /// Returns true if the connectivity index 'connect_group' exists.
536  bool hasConnectGroup(int connect_group) const;
537 
538  bool hasNonEmptyConnectGroup(int connect_group) const;
539 
540  /// Returns the largest index of all the indexed connect groups. Some may
541  /// be NULL.
542  int getMaxConnectGroup() const;
543 
544  /// Remove and delete the connection group specified, return false only if
545  /// the group doesn't exist.
546  bool removeConnectedPrims(int connect_group);
547 
548  /// Removes and deletes all connectivity groups.
549  void resetConnectedPrims();
550 
551  //@}
552 
553  /// Assign a new material to connectivity group 'connect_index'. The
554  /// connectivity group must exist or it will do nothing but return false.
555  bool assignMaterialToConnectivty(RE_Render *r,
556  int connect_index,
557  const RE_MaterialPtr &mat);
558 
559  /// Returns the material attached to the given connectivity group, if any.
560  /// If multiple connectivities were assigned to a group with different
561  /// materials, the subindex is used to select which connectivity is queried.
562  RE_MaterialPtr getConnectivityMaterial(int connect_index,
563  int subindex = 0);
564 
565  /// Methods to control which attributes are used when drawing a
566  /// connect_group. By default, all are enabled. When disabling an array or
567  /// attribute, it will return zero to the vertex shader.
568  //@{
569 
570  /// @brief Toggles the use of a generic vertex attribute for a connect
571  /// group. Index group version of useAttribute(); toggles the attribute
572  /// named 'name' on or off.
573  void useAttribute(int connect_group,
574  const char *name, bool enable);
575  /// @brief Toggles the use of a generic vertex attribute for a connect
576  /// group. Index group version of useAttribute(); toggles the attribute
577  /// specified by 'attrib' on or off.
578  void useAttribute(int connect_group,
579  RE_VertexArray *attrib, bool enable);
580 
581  // ---------------------------------------------------------------------
582 
583  /// Enables or disables textures on materials when drawn
584  void useMaterialTextures(bool enable = true);
585 
586  /// Clamp the number of layers to draw with a multi-layer material
587  void setNumMaterialLayers(int num);
588 
589  // ---------------------------------------------------------------------
590 
591  /// @name Drawing
592  /// These methods draw connectivity groups that were previously set up,
593  /// with some modifications available.
594  /// If 'primtype' is not INVALID, it overrides the connectivity group's
595  /// primitive type. 'attrib_overrides' is a list of string pairs which
596  /// defines a mapping from shader attribute names (indices 0,2,4,6...)
597  /// to geometry attribute names (indices 1,3,5,7...).
598  //@{
599 
600  /// Draw an indexed connectivity group.
601  /// Draws the primitives specified by the cached connectibity info.
602  void draw(RE_Render *r,
603  int connect_idx,
604  RE_PrimType prim_type = RE_PRIM_AS_IS,
605  RE_OverrideList *attrib_overrides = NULL)
606  {
607  drawPrivate(r, getConnect(connect_idx, false),0,
608  prim_type, attrib_overrides, 0);
609  }
610 
611  /// @brief Draw all connectivity groups.
612  /// Draws all connect_groups. Same as looping through all groups and calling
613  /// draw(). The draw order is undefined; if you need a specific order, use
614  /// multiple draw calls.
615  void drawAll(RE_Render *r,
616  RE_PrimType prim_type = RE_PRIM_AS_IS,
617  RE_OverrideList *attrib_overrides = NULL);
618 
619  /// Draws all indexed connect groups in the range
620  /// if 'material_offset' is non-null, it acts as a offset when setting the
621  /// MATERIAL_GROUP uniform, which is added to (index-connect_group_start)
622  /// when drawing indexed connect groups. If num_connected_groups is
623  /// negative, all valid groups >= 'connect_group_start' are drawn,
624  /// otherwise it only draws up to `start+num-1`.
625  void drawRange(RE_Render *r,
626  int connect_group_start, int num_connect_groups,
627  RE_PrimType ptype = RE_PRIM_AS_IS,
628  RE_OverrideList *attrib_overrides = NULL,
629  const int *material_offset = NULL);
630 
631  /// @brief Draw with Instancing
632  /// Draws the same connect group 'num_instances' times. It is up to the
633  /// shader to differentiate the instances, using an instanced attribute
634  /// (createInstancedAttribute()) or GLSL's gl_InstanceID.
635  void drawInstanced(RE_Render *r,
636  int connect_idx,
637  int num_instances,
638  RE_PrimType prim_type = RE_PRIM_AS_IS,
639  RE_OverrideList *attrib_overrides=NULL);
640 
641  /// Draw all connectivity groups using instancing, num_instance times.
642  void drawAllInstanced(RE_Render *r,
643  int num_instances,
644  RE_PrimType prim_type = RE_PRIM_AS_IS,
645  RE_OverrideList *attrib_overrides=0);
646 
647  /// Draw a range of connectivity index groups with instancing
648  void drawRangeInstanced(RE_Render *r,
649  int connect_start, int num_connect,
650  int num_instances,
651  RE_PrimType prim_type = RE_PRIM_AS_IS,
652  RE_OverrideList *attrib_overrides=0,
653  const int *material_offset = NULL);
654 
655  /// Draw an instance group using a given connectivity
656  void drawInstanceGroup(RE_Render *r,
657  int connect_idx,
658  int instance_group,
659  RE_PrimType prim_type = RE_PRIM_AS_IS,
660  RE_OverrideList *attrib_over=NULL);
661 
662  /// Draw an instance group using a given connectivity
663  void drawInstanceGroupRange(RE_Render *r,
664  int connect_start,
665  int num_connect,
666  int instance_group,
667  RE_PrimType prim_type = RE_PRIM_AS_IS,
668  RE_OverrideList *attrib_over=NULL,
669  const int *material_offset = NULL);
670 
671 
672  /// Manually enable and disable arrays. This is useful for determining
673  /// which ones are actually enabled via r->dumpNewState().
674  //@{
675  void enableArrays(RE_Render *r, int connect_group,
676  unsigned int stride = 0);
677  void disableArrays(RE_Render *r, int connect_group);
678  //@}
679 
680  /// @private Used by RE_Shader::prepShader.
681  void bindToVertexState(RE_Render *r,
682  RE_Shader *sh,
683  unsigned int stride,
684  RE_VertexState *vstate,
685  int instance_group);
686 
687  /// resets all VAOs so that on the next draw all VBOs are rebound
688  void resetVertexState(RE_Render *r);
689 
690  /// Remove all arrays and attributes from their GL bindings.
691  void unbindAllArrays(RE_Render *r);
692 
693  // --------------------------------------------------------------------
694  // For debug:
695 
696  // dumps the current configuration of the geometry object (out == NULL will
697  // use std::cerr).
698  void print(std::ostream *out = NULL) const;
699 
700  // For the next draw only, dump uniforms, builtin uniforms, and/or GL state.
701  void setDebugDraw(bool dump_uniforms,
702  bool dump_builtins,
703  bool dump_gl_state);
704 
705 private:
706 
707  RE_VertexArray *getBaseArray(RE_OverrideList *attrib_ovrrides = 0) const;
708 
709  re_Connectivity *getConnect(int group_idx,
710  bool create_if_none);
711 
712  // Clears all the attached buffers. If this object is cached, all cached
713  // vertex arrays/buffers will remain in the cache.
714  void clearBuffers();
715 
716  void bindBuffer(RE_Render *r,
717  RE_VertexArray *array,
718  unsigned int stride,
719  RE_Shader *sh,
720  const char *attrib_name);
721 
722  void unbindBuffer(RE_Render *r,
723  RE_VertexArray *array,
724  RE_Shader *sh,
725  const char *attrib_name);
726 
727  RE_VertexArray *getCachedVertexBuffer(RE_Render *r,
729  int tex_level,
730  const char *attrib_name,
731  RE_ArrayType array_type,
732  RE_GPUType data_format,
733  int vectorsize,
734  int length,
735  RE_VertexArray *target_array,
736  const char *cache_prefix);
737 
738  void freeArray(RE_VertexArray *&a, bool mark_unused = true);
739  void purgeArray(RE_VertexArray *&a, bool mark_unused = true);
740 
741  void assignAttribute(RE_VertexArray *a, int index);
742  void updateKnownAttrib(RE_VertexArray *a, bool set_known);
743  bool privAttachAttribute(RE_VertexArray *attrib, bool show_errors);
744 
745  static bool arrayDeleted(RE_VertexArray *a, void *data);
746 
747  RE_VertexArray * createNewAttribute(RE_Render *r,
748  const char *attrib_name,
749  RE_GPUType data_format,
750  int vectorsize,
751  int instance_stride,
753  int array_size = 0,
756  const char *cache_prefix = NULL,
757  bool create_const_attrib = false);
758 
759  RE_VertexArray *fetchCachedAttrib(RE_Render *r,
760  const char *attrib_name,
761  RE_GPUType data_format,
762  int vectorsize,
763  RE_ArrayType array_type,
764  int inst_step,
765  bool create_if_missing,
766  int array_size,
767  const RE_CacheVersion *cv,
769  const char *cache_prefix=NULL);
770  void drawPrivate(RE_Render *r,
771  re_Connectivity *connect,
772  int num_instances,
773  RE_PrimType prim_type,
774  RE_OverrideList *override_attrib,
775  int instance_group);
776 
777  RE_OGLTexture *prepTexBufferArray(RE_Render *r,
778  RE_VertexArray *array,
779  RE_GPUType buftype,
780  int &pmode);
781  void privEnableArrays(RE_Render *r,
782  re_Connectivity *connect,
783  unsigned int stride = 0,
784  RE_OverrideList *attrib_overrides = NULL);
785  void privDisableArrays(RE_Render *r,
786  re_Connectivity *connect,
787  RE_OverrideList *attrib_overrides = NULL);
788  void privUseAttrib(re_Connectivity *connect,
789  const char *attrib_name,
790  bool enable);
791 
792  void addToInstanceGroup(RE_VertexArray *attrib,
793  int group = -1);
794  void removeFromInstanceGroup(RE_VertexArray *attrib);
795 
796 // DATA:
797  int myNumPoints;
798  int myNumVertices;
799  int myNumPrimitives;
800  RE_VertexMap *myVertexMap;
801  mutable int myAttribPIndex;
802  bool myUseVertexState;
803  bool myPurgeOnDelete;
804  int myVertexStateSerial;
805  UT_String myCacheName;
806  bool myUseTextures;
807  int myNumMaterialLayers;
808 
809  UT_ValArray<RE_VertexArray *> myAttributes;
810 
811  UT_ValArray<RE_VertexArray *> myTextureAttributes;
812  UT_ValArray<RE_Texture *> myBufferTextures;
813 
814  UT_ValArray<RE_VertexArray *> myKnownAttributes;
815  UT_ValArray<RE_VertexArray *> myStashedAttributes;
816 
817  UT_ValArray<re_Connectivity *> myConnectGroups;
818  UT_Array<re_InstanceGroup *> myInstanceGroups;
819 
820  RE_CacheTagHandle myCacheTagHandle;
821 
822  int myDebugDrawFlags;
823 
824  friend class re_InstanceGroup;
825 };
826 
827 inline void
829 {
830  myUseTextures = enable;
831 }
832 
833 inline void
835 {
836  myNumMaterialLayers = SYSclamp(num, 1, 32);
837 }
838 
839 inline RE_VertexArray *
841  const char *attrib_name,
842  RE_GPUType data_format,
843  int vectorsize,
844  RE_ArrayType array_type,
845  bool create_if_missing,
846  int array_size,
847  const RE_CacheVersion *cache_version,
849  const char *cache_prefix)
850 {
851  return fetchCachedAttrib(r, attrib_name, data_format, vectorsize,
852  array_type, 0, create_if_missing,
853  array_size, cache_version, usage,
854  cache_prefix);
855 }
856 
857 inline RE_VertexArray *
859  const char *attrib_name,
860  RE_GPUType data_format,
861  int vectorsize,
862  int inst_step,
863  int array_size,
864  bool create_if_missing,
865  const RE_CacheVersion *cache_version,
867  const char *cache_prefix)
868 {
869  return fetchCachedAttrib(r, attrib_name, data_format, vectorsize,
870  RE_ARRAY_INSTANCE, inst_step,
871  create_if_missing, array_size,
872  cache_version, usage, cache_prefix);
873 }
874 
875 inline RE_VertexArray *
877 {
878  return (id > RE_GENATTRIB_NONE) ? myKnownAttributes(id) : NULL;
879 }
880 
881 #endif
882 
int getNumInstanceGroups() const
Create a new instance group. instance_group must be a positive int.
Definition: RE_Geometry.h:374
RE_VertexArray * findCachedAttrib(RE_Render *r, const char *attrib_name, RE_GPUType data_format, int vectorsize, RE_ArrayType array_type, bool create_if_missing=false, int random_array_size=-1, const RE_CacheVersion *cv=NULL, RE_BufferUsageHint h=RE_BUFFER_WRITE_FREQUENT, const char *cache_prefix=NULL)
Find an attribute or array in the GL cache, possibly creating it Returns the cached array...
Definition: RE_Geometry.h:840
void useMaterialTextures(bool enable=true)
Enables or disables textures on materials when drawn.
Definition: RE_Geometry.h:828
GLsizei GLenum const void * indices
Definition: glcorearb.h:405
#define RE_API
Definition: RE_API.h:10
const GLdouble * v
Definition: glcorearb.h:836
void purgeOnDelete(bool purge=true)
Remove all buffers from the cache when this geometry object is deleted.
Definition: RE_Geometry.h:310
GLuint start
Definition: glcorearb.h:474
void setNumMaterialLayers(int num)
Clamp the number of layers to draw with a multi-layer material.
Definition: RE_Geometry.h:834
RE_VertexArray * findCachedInstancedAttrib(RE_Render *r, const char *attrib_name, RE_GPUType data_format, int vectorsize, int instance_step, int array_size, bool create_if_missing=false, const RE_CacheVersion *v=NULL, RE_BufferUsageHint h=RE_BUFFER_WRITE_FREQUENT, const char *cache_prefix=NULL)
Find an instanced attribute in the GL cache, possibly creating it. The instance_step parameter define...
Definition: RE_Geometry.h:858
A collection of vertex arrays defining a geometry object. This class acts as a wrapper around multipl...
Definition: RE_Geometry.h:53
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
const RE_VertexMap * getVertexMap() const
Return a vertex map representing the layout locations of the attributes.
Definition: RE_Geometry.h:235
bool isCaching() const
Check if this geometry object is caching.
Definition: RE_Geometry.h:307
png_uint_32 i
Definition: png.h:2877
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_Texture * getAddressableAttributeTexture(int index) const
Return the texture buffer object representing addressable attribute 'i'.
Definition: RE_Geometry.h:280
void draw(RE_Render *r, int connect_idx, RE_PrimType prim_type=RE_PRIM_AS_IS, RE_OverrideList *attrib_overrides=NULL)
Definition: RE_Geometry.h:602
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:871
RE_VertexArray * getAddressableAttributeByIndex(int index) const
Return the vertex arrayrepresenting addressable attribute 'i'.
Definition: RE_Geometry.h:283
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
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
GLuint shader
Definition: glcorearb.h:784
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:663
int getNumVertices() const
Sets the number of elements in arrays declared as RE_ARRAY_VERTEX.
Definition: RE_Geometry.h:90
int getNumPoints() const
Number of points in the geometry. Number of points for the arrays declared as RE_ARRAY_POINT. This will clear the data in all point arrays and reset the connectivty.
Definition: RE_Geometry.h:84
GLuint index
Definition: glcorearb.h:785
int getNumAddressableAttributes() const
Return the number of addressable attributes.
Definition: RE_Geometry.h:276
RE_ArrayType
Definition: RE_Types.h:310
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
RE_BufferUsageHint
Definition: RE_Types.h:273
png_infop png_uint_32 int num
Definition: png.h:2158
const char * getCacheName() const
Return the base cache name.
Definition: RE_Geometry.h:304
Simple class for a mutli-integer cache tag.
GLboolean r
Definition: glcorearb.h:1221
RE_VertexArray * getAttribute(const char *name) const
Fetch an attribute by name.
int getNumPrimitives() const
Sets the number of elements in arrays declared as RE_ARRAY_PRIMITIVE.
Definition: RE_Geometry.h:96
RE_PrimType
Definition: RE_Types.h:191
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:794