HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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(
160  RE_Render *r,
161  const char *attrib_name,
162  RE_GPUType data_format,
163  int vectorsize,
164  const void *data,
166  int array_size = 0,
168  const char *cache_prefix = NULL,
169  int capacity = -1);
170 
171  /// @brief Create an instanced vertex attribute attached to 'attrib_name'
172  /// Create an attribute which advances once every 'instance_stride'
173  /// instances rather than once per vertex. RE_EXT_INSTANCED_ARRAYS is
174  /// required, and an instanced drawing method must be used (or it acts as a
175  /// uniform).
176  RE_VertexArray *createInstancedAttribute(RE_Render *r,
177  const char *attrib_name,
178  RE_GPUType data_format,
179  int vectorsize,
180  int instance_stride,
181  int num_instances,
182  const void *data,
183  const char *cache_prefix=NULL,
184  int capacity = -1);
185  /// @brief Create a constant attribute value
186  /// Only RE_GPU_FLOAT32 and _FLOAT64 are supported for constant data.
187  /// The 'data' point must hold at least 1 element (sizeof(type)*vectorsize).
188  RE_VertexArray *createConstAttribute(RE_Render *r,
189  const char *attrib_name,
190  RE_GPUType data_format,
191  int vectorsize,
192  const void *data);
193 
194  /// @brief Create a constant attribute if varying attribute is not found
195  /// Ensures that there is at least a constant attribute value available if
196  /// the attribute doesn't exist already. Only returns an array if one was
197  /// created.
198  RE_VertexArray *assignAttributeDefault(RE_Render *r,
199  const char *attrib_name,
200  RE_GPUType data_format,
201  int vectorsize,
202  const void *default_value);
203 
204  /// Attach an existing attribute using its name as the binding
205  bool attachAttribute(RE_VertexArray *attrib);
206  /// Detatch an attribute from this object by name
207  RE_VertexArray *detachAttribute(const char *name);
208 
209  /// @brief Delete an attached vertex attribute by name
210  /// delete the RE_VertexArray, and if purge_cache is true, remove the
211  /// underlying data buffer from the GL cache.
212  bool clearAttribute(const char *name,
213  bool purge_cache=false);
214 
215  /// @brief Delete an attached vertex attribute by index
216  /// delete the RE_VertexArray, and if purge_cache is true, remove the
217  /// underlying data buffer from the GL cache.
218  bool clearAttributeByIndex(int i,
219  bool purge_cache=false);
220 
221  /// Fetch an attribute by name
222  RE_VertexArray *getAttribute(const char *name) const;
223 
224  /// Return an attribute's index byattribute name. Can change if attributes
225  /// are removed or added.
226  int getAttributeIndex(const char *name) const;
227 
228  /// Fetch an attribute by known type
229  RE_VertexArray *getAttribute(RE_GenericAttribID attrib_id) const;
230 
231  /// Return the number of generic attributes currently attached.
232  int getNumAttributes() const;
233 
234  /// Return currently attached vertex attribute 'i'
235  RE_VertexArray *getAttributeByIndex(int i) const;
236 
237  /// Return a vertex map representing the layout locations of the attributes
238  const RE_VertexMap *getVertexMap() const { return myVertexMap; }
239 
240  //@}
241 
242  // ---------------------------------------------------------------------
243  /// @name Addressable attributes
244  /// Addressable attributes allow you to use buffers, but sample them like
245  /// textures using texelFetch on samplerBuffer uniforms. Only buffer objects
246  /// are supported.
247  //@{
248 
249  /// @brief Create a randomly addressable attribute (texture buffer object)
250  /// Attributes can be stored in texture buffers, and accessed out of
251  /// the normal vertex flow, such as for vertex or primitive attributes.
252  /// Unlike normal attributes, addressable attributes can have a different
253  /// array size than the RE_Geometry's number of vertices.
254  RE_VertexArray *createAddressableAttribute(RE_Render *r,
255  const char *attrib_name,
256  int length,
257  RE_GPUType data_format,
258  int vectorsize,
259  const void *data,
260  RE_ArrayType atype
261  = RE_ARRAY_RANDOM,
262  const char *cache_prefix=NULL);
263  /// Create a randomly addressable attribute from an existing vertex array
264  RE_Texture *createAddressableAttribute(RE_Render *r,
265  const char *attrib_name,
267 
268  /// Fetch an addressable attribute by name
269  RE_VertexArray *getAddressableAttribute(const char *attrib_name) const;
270 
271  /// Return the index of the named addressable attribute, or -1 if not found
272  int getAddressableAttributeIndex(const char *attrib_name)
273  const;
274 
275  /// Delete the addressable attribute named 'attrib_name'
276  void clearAddressableAttribute(const char *attrib_name);
277 
278  /// Return the number of addressable attributes
280  { return myTextureAttributes.entries(); }
281 
282  /// Return the texture buffer object representing addressable attribute 'i'
284  { return myBufferTextures(index); }
285  /// Return the vertex arrayrepresenting addressable attribute 'i'
287  { return myTextureAttributes(index); }
288 
289  //@}
290 
291  // ---------------------------------------------------------------------
292  /// Caching @n
293 
294  /// Use caching for all vertex and element arrays stored by this object.
295  /// If any vertex arrays are attached, they are cleared unless the name
296  /// matches the previous cache name.
297  //@{
298 
299  /// @brief Set the base cache name.
300  /// Attribute names will be concatenated to the base name to form the full
301  /// cache name. Passing NULL will disable caching. Returns true if the
302  /// cachename changed, in which case all existing buffers will be purged
303  /// from the cache.
304  bool cacheBuffers(const char *name);
305 
306  /// Return the base cache name.
307  const char *getCacheName() const { return myCacheName; }
308 
309  /// Check if this geometry object is caching.
310  bool isCaching() const { return myCacheName.isstring(); }
311 
312  /// Remove all buffers from the cache when this geometry object is deleted.
313  void purgeOnDelete(bool purge = true)
314  { myPurgeOnDelete = purge; }
315 
316  /// set the cache version for all arrays attached to this geometry object.
317  void setCacheVersion(RE_CacheVersion v);
318 
319  /// @brief Assign a cache tag to monitor cache changes on this object
320  /// The tag will be bumped if this or any of its vertex buffers are removed
321  /// from the cache.
322  void setCacheTag(RE_CacheTagHandle h);
323 
324  /// @brief Find an attribute or array in the GL cache, possibly creating it
325  /// Returns the cached array, if it exists, for the array named
326  /// 'attrib_name'. The name may be one of the gl-builtin attributes, like
327  /// gl_Position or gl_Color. 'array_size' is the size of an INSTANCE or
328  /// RANDOM array_type.
329  RE_VertexArray *findCachedAttrib(
330  RE_Render *r,
331  const char *attrib_name,
332  RE_GPUType data_format,
333  int vectorsize,
334  RE_ArrayType array_type,
335  bool create_if_missing = false,
336  int random_array_size = -1,
337  const RE_CacheVersion *cv=NULL,
339  const char *cache_prefix = NULL,
340  int capacity = -1);
341 
342  /// @brief Find an instanced attribute in the GL cache, possibly creating
343  /// it. The instance_step parameter defines how often the attribute
344  /// is advanced when the instance ID changes - 1 is once per instance.
345  RE_VertexArray *findCachedInstancedAttrib(
346  RE_Render *r,
347  const char *attrib_name,
348  RE_GPUType data_format,
349  int vectorsize,
350  int instance_step,
351  int array_size,
352  bool create_if_missing=false,
353  const RE_CacheVersion *v=NULL,
355  const char *cache_prefix=NULL,
356  int capacity = -1);
357 
358 
359  //@}
360 
361  // ---------------------------------------------------------------------
362  /// @name Instance groups
363  /// Instance groups allow for rendering of the geometry with different
364  /// attributes, generally InstanceTransform, but others can be overriden
365  /// as well. The base (default) group is instance group 0. Higher groups
366  /// will substitute their attributes for the ones in instance group 0. Note
367  /// that the attribute need not exist in the base group to be overriden.
368  /// If an attribute is not overriden, the base group attribute is used.
369 
370  /// Create a new instance group. instance_group must be a positive int.
371  void createInstanceGroup(int instance_group);
372 
373  /// Remove an existing instance group. Will not remove any attached
374  /// attribute to that group, but the instance group can no longer be
375  /// rendered.
376  void removeInstanceGroup(int instance_group);
377 
378  /// Returns true if the specified instance group exists.
379  bool hasInstanceGroup(int instance_group) const;
380 
382  { return myInstanceGroups.entries(); }
383 
384  /// For instanced drawing, create indirection list to render only some of
385  /// the instances. The base instance group can also be indexed.
386  void setInstanceGroupIndexList(RE_Render *r,
387  int instance_group,
388  bool trivial,
389  const UT_IntArray *indices=NULL,
390  int max_capacity = -1);
391 
392  /// Set to draw a single instance in the group.
393  void setInstanceGroupConstIndex(RE_Render *r,
394  int instance_group,
395  int instance_to_draw);
396 
397  /// Set to draw all instances (based on the # transforms).
398  void setInstanceGroupDrawEverything(RE_Render *r,
399  int instance_group);
400  /// Draw none of the instances.
401  void setInstanceGroupDrawNothing(RE_Render *r,
402  int instance_group);
403 
404 
405  /// Create a constant for an attribute in 'instance_group'.
406  RE_VertexArray *createConstInstanceGroupAttrib(RE_Render *r,
407  int instance_group,
408  const char *name,
409  RE_GPUType data_format,
410  int vectorsize,
411  const void *data);
412 
413  /// Create a constant transform for an instance group.
414  void setConstInstanceGroupTransform(int instance_group,
415  const UT_Matrix4D &xform,
416  bool remove_instanced_xform);
417  void setConstInstanceGroupTransform(int instance_group,
418  const UT_Matrix4F &xform,
419  bool remove_instanced_xform);
420 
421 
422  /// @brief Find an instance-group attribute in the cache
423  /// Creates an instanced attribute for an instance-group, which overrides
424  /// the base attribute on the RE_Geometry. instance_group zero is the base
425  /// level ("Cd"), all other groups have the group appended ("Cd1" for
426  /// group 1). When drawn with drawInstanceGroup(), attributes in the
427  /// specified instance group will be used (if present) instead of the base
428  /// attributes.
429  RE_VertexArray *findCachedInstanceGroupAttrib(RE_Render *r,
430  int instance_group,
431  const char *name,
432  RE_GPUType data_type,
433  int vector_size,
434  int instance_step,
435  int num_instances,
436  bool create=false,
437  const RE_CacheVersion *v=NULL,
440  const char *view_name = NULL,
441  int capacity = -1);
442 
443  /// Return the attribute in the instance group with type 'attrib_id', or
444  /// if it's not a known type, by name.
445  RE_VertexArray *getInstanceGroupAttrib(int instance_group,
446  RE_GenericAttribID attrib_id,
447  const char *name);
448  /// Remove and delete the array.
449  bool clearInstanceGroupAttrib(int instance_group,
450  RE_GenericAttribID attrib_id,
451  const char *name);
452 
453  /// This is the same as getInstanceGroupAttrib(), but it used mainly by
454  /// RE_Shader to do additional prep on the attrib.
455  RE_VertexArray *getInstanceGroupTextureBufferForShader(RE_Render *r,
456  RE_Shader *sh,
457  int instance_group,
458  RE_GenericAttribID attr_id,
459  const char *name);
460 
461  /// Returns the number of instances to be drawn in an instance group.
462  int getInstanceGroupCount(int instance_group) const;
463 
464  /// Return a full instance group name based on an original base attrib name
465  static void getInstanceGroupName(UT_WorkBuffer &inst_name,
466  const char *base_name,
467  int instance_group);
468 
469  /// Return a full instance group name based on the known attribute type
470  static void getInstanceGroupName(UT_WorkBuffer &inst_name,
471  RE_GenericAttribID attrib_type,
472  int instance_group);
473 
474  // ---------------------------------------------------------------------
475  /// Primitive Connectivity @n
476 
477  /// These methods allow you to set up and cache primitive connectivity.
478  /// Each bit of primitive connectivity is added to a named group, like
479  /// 'wireframe' or 'shaded'. A connectivity group can have multiple
480  /// types of primitives or materials. Groups are indexed, using int ids.
481  /// You can pass an optional material to each set of connected prims. If
482  /// the material is null, it will use the last used material (which may be
483  /// the material of earlier connected primitives). Materials are not owned
484  /// by this class; the element arrays are. If 'replace' is true, any
485  /// previous connectivity established with the name 'connect_group'
486  /// will be removed and replaced with the new connectivity.
487  //@{
488 
489  /// Connect all primtives and place them in indexed group 'connect_index'
490  int connectAllPrims(RE_Render *r,
491  int connect_index,
492  RE_PrimType prim,
493  const RE_MaterialPtr &mat = NULL,
494  bool replace = false,
495  int vertices_per_patch = 0);
496 
497  /// Connect a subrange of vertices and place in index group 'connect_index'
498  int connectSomePrims(RE_Render *r,
499  int connect_group,
500  RE_PrimType prim,
501  int start,
502  int length,
503  unsigned int stride = 0,
504  const RE_MaterialPtr &mat = NULL,
505  bool replace = false,
506  int vertices_per_patch = 0);
507 
508  /// @brief Connect vertices using an indexed vertex list, add to 'connect_group'.
509  /// 'num' is the size of the list, not the number of primitives. If
510  /// any index is greater than the number of points in the geometry,
511  /// the results will be undefined. This is added to connect_group.
512  int connectIndexedPrims(RE_Render *r,
513  int connect_group,
514  RE_PrimType prim,
515  int num,
516  const unsigned int *prims,
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 buffer object 'elements'
522  int connectIndexedPrims(RE_Render *r,
523  int connect_group,
524  RE_PrimType prim,
525  RE_VertexArray *elements,
526  const RE_MaterialPtr &mat = NULL,
527  bool replace = false,
528  int vertices_per_patch = 0);
529 
530  /// Connect vertices using the indices in the element array 'elements'
531  int connectIndexedPrims(RE_Render *r,
532  int connect_group,
533  RE_ElementArray *elements,
534  const RE_MaterialPtr &mat = NULL,
535  bool replace = false);
536 
537 
538  /// Returns the number of discrete connectivities in 'connect_group'
539  int getConnectNumElementArrays(int connect_group);
540  /// Returns the element array associated with connect_group, if any.
541  RE_ElementArray *getConnectElementArray(int connect_group,
542  int index = 0);
543 
544  /// Returns true if the connectivity index 'connect_group' exists.
545  bool hasConnectGroup(int connect_group) const;
546 
547  bool hasNonEmptyConnectGroup(int connect_group) const;
548 
549  /// Returns the largest index of all the indexed connect groups. Some may
550  /// be NULL.
551  int getMaxConnectGroup() const;
552 
553  /// Remove and delete the connection group specified, return false only if
554  /// the group doesn't exist.
555  bool removeConnectedPrims(int connect_group);
556 
557  /// Removes and deletes all connectivity groups.
558  void resetConnectedPrims();
559 
560  //@}
561 
562  /// Assign a new material to connectivity group 'connect_index'. The
563  /// connectivity group must exist or it will do nothing but return false.
564  bool assignMaterialToConnectivty(RE_Render *r,
565  int connect_index,
566  const RE_MaterialPtr &mat);
567 
568  /// Returns the material attached to the given connectivity group, if any.
569  /// If multiple connectivities were assigned to a group with different
570  /// materials, the subindex is used to select which connectivity is queried.
571  RE_MaterialPtr getConnectivityMaterial(int connect_index,
572  int subindex = 0);
573 
574  /// Methods to control which attributes are used when drawing a
575  /// connect_group. By default, all are enabled. When disabling an array or
576  /// attribute, it will return zero to the vertex shader.
577  //@{
578 
579  /// @brief Toggles the use of a generic vertex attribute for a connect
580  /// group. Index group version of useAttribute(); toggles the attribute
581  /// named 'name' on or off.
582  void useAttribute(int connect_group,
583  const char *name, bool enable);
584  /// @brief Toggles the use of a generic vertex attribute for a connect
585  /// group. Index group version of useAttribute(); toggles the attribute
586  /// specified by 'attrib' on or off.
587  void useAttribute(int connect_group,
588  RE_VertexArray *attrib, bool enable);
589 
590  // ---------------------------------------------------------------------
591 
592  /// Enables or disables textures on materials when drawn
593  void useMaterialTextures(bool enable = true);
594 
595  /// Clamp the number of layers to draw with a multi-layer material
596  void setNumMaterialLayers(int num);
597 
598  //@}
599 
600  // ---------------------------------------------------------------------
601 
602  /// @name Drawing
603  /// These methods draw connectivity groups that were previously set up,
604  /// with some modifications available.
605  /// If 'primtype' is not INVALID, it overrides the connectivity group's
606  /// primitive type. 'attrib_overrides' is a list of string pairs which
607  /// defines a mapping from shader attribute names (indices 0,2,4,6...)
608  /// to geometry attribute names (indices 1,3,5,7...).
609  //@{
610 
611  /// Draw an indexed connectivity group.
612  /// Draws the primitives specified by the cached connectibity info.
613  void draw(RE_Render *r,
614  int connect_idx,
615  RE_PrimType prim_type = RE_PRIM_AS_IS,
616  RE_OverrideList *attrib_overrides = NULL)
617  {
618  drawPrivate(r, getConnect(connect_idx, false),0,
619  prim_type, attrib_overrides, 0);
620  }
621 
622  /// @brief Draw all connectivity groups.
623  /// Draws all connect_groups. Same as looping through all groups and calling
624  /// draw(). The draw order is undefined; if you need a specific order, use
625  /// multiple draw calls.
626  void drawAll(RE_Render *r,
627  RE_PrimType prim_type = RE_PRIM_AS_IS,
628  RE_OverrideList *attrib_overrides = NULL);
629 
630  /// Draws all indexed connect groups in the range
631  /// if 'material_offset' is non-null, it acts as a offset when setting the
632  /// MATERIAL_GROUP uniform, which is added to (index-connect_group_start)
633  /// when drawing indexed connect groups. If num_connected_groups is
634  /// negative, all valid groups >= 'connect_group_start' are drawn,
635  /// otherwise it only draws up to `start+num-1`.
636  void drawRange(RE_Render *r,
637  int connect_group_start, int num_connect_groups,
638  RE_PrimType ptype = RE_PRIM_AS_IS,
639  RE_OverrideList *attrib_overrides = NULL,
640  const int *material_offset = NULL);
641 
642  /// @brief Draw with Instancing
643  /// Draws the same connect group 'num_instances' times. It is up to the
644  /// shader to differentiate the instances, using an instanced attribute
645  /// (createInstancedAttribute()) or GLSL's gl_InstanceID.
646  void drawInstanced(RE_Render *r,
647  int connect_idx,
648  int num_instances,
649  RE_PrimType prim_type = RE_PRIM_AS_IS,
650  RE_OverrideList *attrib_overrides=NULL);
651 
652  /// Draw all connectivity groups using instancing, num_instance times.
653  void drawAllInstanced(RE_Render *r,
654  int num_instances,
655  RE_PrimType prim_type = RE_PRIM_AS_IS,
656  RE_OverrideList *attrib_overrides=0);
657 
658  /// Draw a range of connectivity index groups with instancing
659  void drawRangeInstanced(RE_Render *r,
660  int connect_start, int num_connect,
661  int num_instances,
662  RE_PrimType prim_type = RE_PRIM_AS_IS,
663  RE_OverrideList *attrib_overrides=0,
664  const int *material_offset = NULL);
665 
666  /// Draw an instance group using a given connectivity
667  void drawInstanceGroup(RE_Render *r,
668  int connect_idx,
669  int instance_group,
670  RE_PrimType prim_type = RE_PRIM_AS_IS,
671  RE_OverrideList *attrib_over=NULL);
672 
673  /// Draw an instance group using a given connectivity
674  void drawInstanceGroupRange(RE_Render *r,
675  int connect_start,
676  int num_connect,
677  int instance_group,
678  RE_PrimType prim_type = RE_PRIM_AS_IS,
679  RE_OverrideList *attrib_over=NULL,
680  const int *material_offset = NULL);
681 
682  //@}
683 
684  /// Manually enable and disable arrays. This is useful for determining
685  /// which ones are actually enabled via r->dumpNewState().
686  //@{
687  void enableArrays(RE_Render *r, int connect_group,
688  unsigned int stride = 0);
689  void disableArrays(RE_Render *r, int connect_group);
690  //@}
691 
692  /// @private Used by RE_Shader::prepShader.
693  void bindToVertexState(RE_Render *r,
694  RE_Shader *sh,
695  unsigned int stride,
696  RE_VertexState *vstate,
697  int instance_group);
698 
699  /// resets all VAOs so that on the next draw all VBOs are rebound
700  void resetVertexState(RE_Render *r);
701 
702  /// Remove all arrays and attributes from their GL bindings.
703  void unbindAllArrays(RE_Render *r);
704 
705  // --------------------------------------------------------------------
706  // For debug:
707 
708  // dumps the current configuration of the geometry object (out == NULL will
709  // use std::cerr).
710  void print(std::ostream *out = NULL) const;
711 
712  // For the next draw only, dump uniforms, builtin uniforms, and/or GL state.
713  void setDebugDraw(bool dump_uniforms,
714  bool dump_builtins,
715  bool dump_gl_state);
716 
717 private:
718 
719  RE_VertexArray *getBaseArray(RE_OverrideList *attrib_ovrrides = 0) const;
720 
721  re_Connectivity *getConnect(int group_idx,
722  bool create_if_none);
723 
724  // Clears all the attached buffers. If this object is cached, all cached
725  // vertex arrays/buffers will remain in the cache.
726  void clearBuffers(bool connectivity_too = true);
727 
728  void bindBuffer(RE_Render *r,
729  RE_VertexArray *array,
730  unsigned int stride,
731  RE_Shader *sh,
732  const char *attrib_name);
733 
734  void unbindBuffer(RE_Render *r,
735  RE_VertexArray *array,
736  RE_Shader *sh,
737  const char *attrib_name);
738 
739  RE_VertexArray *getCachedVertexBuffer(RE_Render *r,
741  int tex_level,
742  const char *attrib_name,
743  RE_ArrayType array_type,
744  RE_GPUType data_format,
745  int vectorsize,
746  int length,
747  RE_VertexArray *target_array,
748  const char *cache_prefix);
749 
750  void freeArray(RE_VertexArray *&a, bool mark_unused = true);
751  void purgeArray(RE_VertexArray *&a, bool mark_unused = true);
752 
753  void assignAttribute(RE_VertexArray *a, int index);
754  void updateKnownAttrib(RE_VertexArray *a, bool set_known);
755  bool privAttachAttribute(RE_VertexArray *attrib, bool show_errors);
756 
757  static bool arrayDeleted(RE_VertexArray *a, void *data);
758 
759  RE_VertexArray * createNewAttribute(RE_Render *r,
760  const char *attrib_name,
761  RE_GPUType data_format,
762  int vectorsize,
763  int instance_stride,
765  int array_size = 0,
768  const char *cache_prefix = NULL,
769  bool create_const_attrib = false,
770  int array_capacity = -1);
771 
772  RE_VertexArray *fetchCachedAttrib(RE_Render *r,
773  const char *attrib_name,
774  RE_GPUType data_format,
775  int vectorsize,
776  RE_ArrayType array_type,
777  int inst_step,
778  bool create_if_missing,
779  int array_size,
780  const RE_CacheVersion *cv,
782  const char *cache_prefix=NULL,
783  int capacity = -1);
784  void drawPrivate(RE_Render *r,
785  re_Connectivity *connect,
786  int num_instances,
787  RE_PrimType prim_type,
788  RE_OverrideList *override_attrib,
789  int instance_group);
790 
791  RE_OGLTexture *prepTexBufferArray(RE_Render *r,
792  RE_VertexArray *array,
793  RE_GPUType buftype,
794  int &pmode);
795  void privEnableArrays(
796  RE_Render *r,
797  re_Connectivity *connect,
798  unsigned int stride = 0,
799  RE_OverrideList *attrib_overrides = NULL);
800  void privDisableArrays(
801  RE_Render *r,
802  re_Connectivity *connect,
803  RE_OverrideList *attrib_overrides = NULL);
804  void privUseAttrib(
805  re_Connectivity *connect,
806  const char *attrib_name,
807  bool enable);
808 
809  void addToInstanceGroup(RE_VertexArray *attrib,
810  int group = -1);
811  void removeFromInstanceGroup(RE_VertexArray *attrib);
812 
813 // DATA:
814  int myNumPoints;
815  int myNumVertices;
816  int myNumPrimitives;
817  RE_VertexMap *myVertexMap;
818  mutable int myAttribPIndex;
819  bool myUseVertexState;
820  bool myPurgeOnDelete;
821  int myVertexStateSerial;
822  UT_String myCacheName;
823  bool myUseTextures;
824  int myNumMaterialLayers;
825 
826  UT_ValArray<RE_VertexArray *> myAttributes;
827 
828  UT_ValArray<RE_VertexArray *> myTextureAttributes;
829  UT_ValArray<RE_Texture *> myBufferTextures;
830 
831  UT_ValArray<RE_VertexArray *> myKnownAttributes;
832  UT_ValArray<RE_VertexArray *> myStashedAttributes;
833 
834  UT_ValArray<re_Connectivity *> myConnectGroups;
835  UT_Array<re_InstanceGroup *> myInstanceGroups;
836 
837  RE_CacheTagHandle myCacheTagHandle;
838 
839  int myDebugDrawFlags;
840 
841  friend class re_InstanceGroup;
842 };
843 
844 inline void
846 {
847  myUseTextures = enable;
848 }
849 
850 inline void
852 {
853  myNumMaterialLayers = SYSclamp(num, 1, 32);
854 }
855 
856 inline RE_VertexArray *
858  const char *attrib_name,
859  RE_GPUType data_format,
860  int vectorsize,
861  RE_ArrayType array_type,
862  bool create_if_missing,
863  int array_size,
864  const RE_CacheVersion *cache_version,
866  const char *cache_prefix,
867  int capacity)
868 {
869  return fetchCachedAttrib(r, attrib_name, data_format, vectorsize,
870  array_type, 0, create_if_missing,
871  array_size, cache_version, usage,
872  cache_prefix, capacity);
873 }
874 
875 inline RE_VertexArray *
877  const char *attrib_name,
878  RE_GPUType data_format,
879  int vectorsize,
880  int inst_step,
881  int array_size,
882  bool create_if_missing,
883  const RE_CacheVersion *cache_version,
885  const char *cache_prefix,
886  int capacity)
887 {
888  return fetchCachedAttrib(r, attrib_name, data_format, vectorsize,
889  RE_ARRAY_INSTANCE, inst_step,
890  create_if_missing, array_size,
891  cache_version, usage, cache_prefix, capacity);
892 }
893 
894 inline RE_VertexArray *
896 {
897  return (id > RE_GENATTRIB_NONE) ? myKnownAttributes(id) : NULL;
898 }
899 
900 #endif
901 
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, int capacity=-1)
Find an attribute or array in the GL cache, possibly creating it Returns the cached array...
Definition: RE_Geometry.h:857
int getNumInstanceGroups() const
Create a new instance group. instance_group must be a positive int.
Definition: RE_Geometry.h:381
void useMaterialTextures(bool enable=true)
Enables or disables textures on materials when drawn.
Definition: RE_Geometry.h:845
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:313
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:851
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:238
bool isCaching() const
Check if this geometry object is caching.
Definition: RE_Geometry.h:310
png_uint_32 i
Definition: png.h:2877
RE_GenericAttribID
Definition: RE_Types.h:323
long long int64
Definition: SYS_Types.h:107
RE_GPUType
Definition: RE_Types.h:44
RE_BufferType
Definition: RE_Types.h:264
RE_Texture * getAddressableAttributeTexture(int index) const
Return the texture buffer object representing addressable attribute 'i'.
Definition: RE_Geometry.h:283
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:613
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:286
GLboolean * data
Definition: glcorearb.h:130
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:318
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
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, int capacity=-1)
Find an instanced attribute in the GL cache, possibly creating it. The instance_step parameter define...
Definition: RE_Geometry.h:876
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:279
RE_ArrayType
Definition: RE_Types.h:312
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
RE_BufferUsageHint
Definition: RE_Types.h:275
png_infop png_uint_32 int num
Definition: png.h:2158
const char * getCacheName() const
Return the base cache name.
Definition: RE_Geometry.h:307
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:193
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:794