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  /// Use nested instancing, rather than flattening data into arrays of
385  /// length N*M (for M instances nested within N instances). This requires
386  /// data arrays of size N+M. Element 0 contains the count of the instances
387  /// in the topmost level (N). Up to 9 levels are supported.
388  /// Instance indices are still expected in the range [0, N*M)
389  void setInstanceGroupNesting(int instance_group,
390  const UT_IntArray &count_per_level);
391  void clearInstanceGroupNesting(int instance_group);
392 
393  /// For instanced drawing, create indirection list to render only some of
394  /// the instances. The base instance group can also be indexed.
395  void setInstanceGroupIndexList(RE_Render *r,
396  int instance_group,
397  bool trivial,
398  const UT_IntArray *indices=NULL,
399  int max_capacity = -1);
400 
401  /// Set to draw a single instance in the group.
402  void setInstanceGroupConstIndex(RE_Render *r,
403  int instance_group,
404  int instance_to_draw);
405 
406  /// Set to draw all instances (based on the # transforms).
407  void setInstanceGroupDrawEverything(RE_Render *r,
408  int instance_group);
409  /// Draw none of the instances.
410  void setInstanceGroupDrawNothing(RE_Render *r,
411  int instance_group);
412 
413 
414  /// Create a constant for an attribute in 'instance_group'.
415  RE_VertexArray *createConstInstanceGroupAttrib(RE_Render *r,
416  int instance_group,
417  const char *name,
418  RE_GPUType data_format,
419  int vectorsize,
420  const void *data);
421 
422  /// Create a constant transform for an instance group.
423  void setConstInstanceGroupTransform(int instance_group,
424  const UT_Matrix4D &xform,
425  bool remove_instanced_xform);
426  void setConstInstanceGroupTransform(int instance_group,
427  const UT_Matrix4F &xform,
428  bool remove_instanced_xform);
429 
430 
431  /// @brief Find an instance-group attribute in the cache
432  /// Creates an instanced attribute for an instance-group, which overrides
433  /// the base attribute on the RE_Geometry. instance_group zero is the base
434  /// level ("Cd"), all other groups have the group appended ("Cd1" for
435  /// group 1). When drawn with drawInstanceGroup(), attributes in the
436  /// specified instance group will be used (if present) instead of the base
437  /// attributes.
438  RE_VertexArray *findCachedInstanceGroupAttrib(RE_Render *r,
439  int instance_group,
440  const char *name,
441  RE_GPUType data_type,
442  int vector_size,
443  int instance_step,
444  int num_instances,
445  bool create=false,
446  const RE_CacheVersion *v=NULL,
449  const char *view_name = NULL,
450  int capacity = -1);
451 
452  /// Return the attribute in the instance group with type 'attrib_id', or
453  /// if it's not a known type, by name.
454  RE_VertexArray *getInstanceGroupAttrib(int instance_group,
455  RE_GenericAttribID attrib_id,
456  const char *name);
457  /// Remove and delete the array.
458  bool clearInstanceGroupAttrib(int instance_group,
459  RE_GenericAttribID attrib_id,
460  const char *name);
461 
462  /// This is the same as getInstanceGroupAttrib(), but it used mainly by
463  /// RE_Shader to do additional prep on the attrib.
464  RE_VertexArray *getInstanceGroupTextureBufferForShader(RE_Render *r,
465  RE_Shader *sh,
466  int instance_group,
467  RE_GenericAttribID attr_id,
468  const char *name);
469 
470  /// Returns the number of instances to be drawn in an instance group.
471  int getInstanceGroupCount(int instance_group) const;
472 
473  /// Return a full instance group name based on an original base attrib name
474  static void getInstanceGroupName(UT_WorkBuffer &inst_name,
475  const char *base_name,
476  int instance_group);
477 
478  /// Return a full instance group name based on the known attribute type
479  static void getInstanceGroupName(UT_WorkBuffer &inst_name,
480  RE_GenericAttribID attrib_type,
481  int instance_group);
482 
483  // ---------------------------------------------------------------------
484  /// Primitive Connectivity @n
485 
486  /// These methods allow you to set up and cache primitive connectivity.
487  /// Each bit of primitive connectivity is added to a named group, like
488  /// 'wireframe' or 'shaded'. A connectivity group can have multiple
489  /// types of primitives or materials. Groups are indexed, using int ids.
490  /// You can pass an optional material to each set of connected prims. If
491  /// the material is null, it will use the last used material (which may be
492  /// the material of earlier connected primitives). Materials are not owned
493  /// by this class; the element arrays are. If 'replace' is true, any
494  /// previous connectivity established with the name 'connect_group'
495  /// will be removed and replaced with the new connectivity.
496  //@{
497 
498  /// Connect all primitives and place them in indexed group 'connect_index'
499  int connectAllPrims(RE_Render *r,
500  int connect_index,
501  RE_PrimType prim,
502  const RE_MaterialPtr &mat = NULL,
503  bool replace = false,
504  int vertices_per_patch = 0);
505 
506  /// Connect a subrange of vertices and place in index group 'connect_index'
507  int connectSomePrims(RE_Render *r,
508  int connect_group,
509  RE_PrimType prim,
510  int start,
511  int length,
512  unsigned int stride = 0,
513  const RE_MaterialPtr &mat = NULL,
514  bool replace = false,
515  int vertices_per_patch = 0);
516 
517  /// @brief Connect vertices using an indexed vertex list, add to 'connect_group'.
518  /// 'num' is the size of the list, not the number of primitives. If
519  /// any index is greater than the number of points in the geometry,
520  /// the results will be undefined. This is added to connect_group.
521  int connectIndexedPrims(RE_Render *r,
522  int connect_group,
523  RE_PrimType prim,
524  int num,
525  const unsigned int *prims,
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 buffer object 'elements'
531  int connectIndexedPrims(RE_Render *r,
532  int connect_group,
533  RE_PrimType prim,
534  RE_VertexArray *elements,
535  const RE_MaterialPtr &mat = NULL,
536  bool replace = false,
537  int vertices_per_patch = 0);
538 
539  /// Connect vertices using the indices in the element array 'elements'
540  int connectIndexedPrims(RE_Render *r,
541  int connect_group,
542  RE_ElementArray *elements,
543  const RE_MaterialPtr &mat = NULL,
544  bool replace = false);
545 
546 
547  /// Returns the number of discrete connectivities in 'connect_group'
548  int getConnectNumElementArrays(int connect_group);
549  /// Returns the element array associated with connect_group, if any.
550  RE_ElementArray *getConnectElementArray(int connect_group,
551  int index = 0);
552 
553  /// Returns true if the connectivity index 'connect_group' exists.
554  bool hasConnectGroup(int connect_group) const;
555 
556  bool hasNonEmptyConnectGroup(int connect_group) const;
557 
558  /// Returns the largest index of all the indexed connect groups. Some may
559  /// be NULL.
560  int getMaxConnectGroup() const;
561 
562  /// Remove and delete the connection group specified, return false only if
563  /// the group doesn't exist.
564  bool removeConnectedPrims(int connect_group);
565 
566  /// Removes and deletes all connectivity groups.
567  void resetConnectedPrims();
568 
569  //@}
570 
571  /// Assign a new material to connectivity group 'connect_index'. The
572  /// connectivity group must exist or it will do nothing but return false.
573  bool assignMaterialToConnectivty(RE_Render *r,
574  int connect_index,
575  const RE_MaterialPtr &mat);
576 
577  /// Returns the material attached to the given connectivity group, if any.
578  /// If multiple connectivities were assigned to a group with different
579  /// materials, the subindex is used to select which connectivity is queried.
580  RE_MaterialPtr getConnectivityMaterial(int connect_index,
581  int subindex = 0);
582 
583  /// Methods to control which attributes are used when drawing a
584  /// connect_group. By default, all are enabled. When disabling an array or
585  /// attribute, it will return zero to the vertex shader.
586  //@{
587 
588  /// @brief Toggles the use of a generic vertex attribute for a connect
589  /// group. Index group version of useAttribute(); toggles the attribute
590  /// named 'name' on or off.
591  void useAttribute(int connect_group,
592  const char *name, bool enable);
593  /// @brief Toggles the use of a generic vertex attribute for a connect
594  /// group. Index group version of useAttribute(); toggles the attribute
595  /// specified by 'attrib' on or off.
596  void useAttribute(int connect_group,
597  RE_VertexArray *attrib, bool enable);
598 
599  // ---------------------------------------------------------------------
600 
601  /// Enables or disables textures on materials when drawn
602  void useMaterialTextures(bool enable = true);
603 
604  /// Clamp the number of layers to draw with a multi-layer material
605  void setNumMaterialLayers(int num);
606 
607  //@}
608 
609  // ---------------------------------------------------------------------
610 
611  /// @name Drawing
612  /// These methods draw connectivity groups that were previously set up,
613  /// with some modifications available.
614  /// If 'primtype' is not INVALID, it overrides the connectivity group's
615  /// primitive type. 'attrib_overrides' is a list of string pairs which
616  /// defines a mapping from shader attribute names (indices 0,2,4,6...)
617  /// to geometry attribute names (indices 1,3,5,7...).
618  //@{
619 
620  /// Draw an indexed connectivity group.
621  /// Draws the primitives specified by the cached connectibity info.
622  void draw(RE_Render *r,
623  int connect_idx,
624  RE_PrimType prim_type = RE_PRIM_AS_IS,
625  RE_OverrideList *attrib_overrides = NULL)
626  {
627  drawPrivate(r, getConnect(connect_idx, false),0,
628  prim_type, attrib_overrides, 0);
629  }
630 
631  /// @brief Draw all connectivity groups.
632  /// Draws all connect_groups. Same as looping through all groups and calling
633  /// draw(). The draw order is undefined; if you need a specific order, use
634  /// multiple draw calls.
635  void drawAll(RE_Render *r,
636  RE_PrimType prim_type = RE_PRIM_AS_IS,
637  RE_OverrideList *attrib_overrides = NULL);
638 
639  /// Draws all indexed connect groups in the range
640  /// if 'material_offset' is non-null, it acts as a offset when setting the
641  /// MATERIAL_GROUP uniform, which is added to (index-connect_group_start)
642  /// when drawing indexed connect groups. If num_connected_groups is
643  /// negative, all valid groups >= 'connect_group_start' are drawn,
644  /// otherwise it only draws up to `start+num-1`.
645  void drawRange(RE_Render *r,
646  int connect_group_start, int num_connect_groups,
647  RE_PrimType ptype = RE_PRIM_AS_IS,
648  RE_OverrideList *attrib_overrides = NULL,
649  const int *material_offset = NULL);
650 
651  /// @brief Draw with Instancing
652  /// Draws the same connect group 'num_instances' times. It is up to the
653  /// shader to differentiate the instances, using an instanced attribute
654  /// (createInstancedAttribute()) or GLSL's gl_InstanceID.
655  void drawInstanced(RE_Render *r,
656  int connect_idx,
657  int num_instances,
658  RE_PrimType prim_type = RE_PRIM_AS_IS,
659  RE_OverrideList *attrib_overrides=NULL);
660 
661  /// Draw all connectivity groups using instancing, num_instance times.
662  void drawAllInstanced(RE_Render *r,
663  int num_instances,
664  RE_PrimType prim_type = RE_PRIM_AS_IS,
665  RE_OverrideList *attrib_overrides=0);
666 
667  /// Draw a range of connectivity index groups with instancing
668  void drawRangeInstanced(RE_Render *r,
669  int connect_start, int num_connect,
670  int num_instances,
671  RE_PrimType prim_type = RE_PRIM_AS_IS,
672  RE_OverrideList *attrib_overrides=0,
673  const int *material_offset = NULL);
674 
675  /// Draw an instance group using a given connectivity
676  void drawInstanceGroup(RE_Render *r,
677  int connect_idx,
678  int instance_group,
679  RE_PrimType prim_type = RE_PRIM_AS_IS,
680  RE_OverrideList *attrib_over=NULL);
681 
682  /// Draw an instance group using a given connectivity
683  void drawInstanceGroupRange(RE_Render *r,
684  int connect_start,
685  int num_connect,
686  int instance_group,
687  RE_PrimType prim_type = RE_PRIM_AS_IS,
688  RE_OverrideList *attrib_over=NULL,
689  const int *material_offset = NULL);
690  // Draw using parms from an indirect buffer (4 units).
691  // See GL_ARB_draw_indirect for more info. RE_EXT_DRAW_INDIRECT must be
692  // supported. The #prims & draw count in the connect group are ignored.
693  void drawIndirect(RE_Render *r,
694  int connect_group,
695  RE_VertexArray &indirect_buffer);
696 
697  //@}
698 
699  /// Manually enable and disable arrays. This is useful for determining
700  /// which ones are actually enabled via r->dumpNewState().
701  //@{
702  void enableArrays(RE_Render *r, int connect_group,
703  unsigned int stride = 0);
704  void disableArrays(RE_Render *r, int connect_group);
705  //@}
706 
707  /// @private Used by RE_Shader::prepShader.
708  void bindToVertexState(RE_Render *r,
709  RE_Shader *sh,
710  unsigned int stride,
711  RE_VertexState *vstate,
712  int instance_group);
713 
714  /// resets all VAOs so that on the next draw all VBOs are rebound
715  void resetVertexState(RE_Render *r);
716 
717  /// Remove all arrays and attributes from their GL bindings.
718  void unbindAllArrays(RE_Render *r);
719 
720  // --------------------------------------------------------------------
721  // For debug:
722 
723  // dumps the current configuration of the geometry object (out == NULL will
724  // use std::cerr).
725  void print(std::ostream *out = NULL) const;
726 
727  // For the next draw only, dump uniforms, builtin uniforms, and/or GL state.
728  void setDebugDraw(bool dump_uniforms,
729  bool dump_builtins,
730  bool dump_gl_state);
731 
732 private:
733 
734  RE_VertexArray *getBaseArray(RE_OverrideList *attrib_ovrrides = 0) const;
735 
736  re_Connectivity *getConnect(int group_idx,
737  bool create_if_none);
738 
739  // Clears all the attached buffers. If this object is cached, all cached
740  // vertex arrays/buffers will remain in the cache.
741  void clearBuffers(bool connectivity_too = true);
742 
743  void bindBuffer(RE_Render *r,
745  unsigned int stride,
746  RE_Shader *sh,
747  const char *attrib_name);
748 
749  void unbindBuffer(RE_Render *r,
751  RE_Shader *sh,
752  const char *attrib_name);
753 
754  RE_VertexArray *getCachedVertexBuffer(RE_Render *r,
756  int tex_level,
757  const char *attrib_name,
758  RE_ArrayType array_type,
759  RE_GPUType data_format,
760  int vectorsize,
761  int length,
762  RE_VertexArray *target_array,
763  const char *cache_prefix);
764 
765  void freeArray(RE_VertexArray *&a, bool mark_unused = true);
766  void purgeArray(RE_VertexArray *&a, bool mark_unused = true);
767 
768  void assignAttribute(RE_VertexArray *a, int index);
769  void updateKnownAttrib(RE_VertexArray *a, bool set_known);
770  bool privAttachAttribute(RE_VertexArray *attrib, bool show_errors);
771 
772  static bool arrayDeleted(RE_VertexArray *a, void *data);
773 
774  RE_VertexArray * createNewAttribute(RE_Render *r,
775  const char *attrib_name,
776  RE_GPUType data_format,
777  int vectorsize,
778  int instance_stride,
780  int array_size = 0,
783  const char *cache_prefix = NULL,
784  bool create_const_attrib = false,
785  int array_capacity = -1);
786 
787  RE_VertexArray *fetchCachedAttrib(RE_Render *r,
788  const char *attrib_name,
789  RE_GPUType data_format,
790  int vectorsize,
791  RE_ArrayType array_type,
792  int inst_step,
793  bool create_if_missing,
794  int array_size,
795  const RE_CacheVersion *cv,
797  const char *cache_prefix=NULL,
798  int capacity = -1);
799  void drawPrivate(RE_Render *r,
800  re_Connectivity *connect,
801  int num_instances,
802  RE_PrimType prim_type,
803  RE_OverrideList *override_attrib,
804  int instance_group,
805  bool indirect = false);
806 
807  RE_OGLTexture *prepTexBufferArray(RE_Render *r,
809  RE_GPUType buftype,
810  int &pmode);
811  void privEnableArrays(
812  RE_Render *r,
813  re_Connectivity *connect,
814  unsigned int stride = 0,
815  RE_OverrideList *attrib_overrides = NULL,
816  int instance_group = 0);
817  void privDisableArrays(
818  RE_Render *r,
819  re_Connectivity *connect,
820  RE_OverrideList *attrib_overrides = NULL);
821  void privUseAttrib(
822  re_Connectivity *connect,
823  const char *attrib_name,
824  bool enable);
825 
826  void addToInstanceGroup(RE_VertexArray *attrib,
827  int group = -1);
828  void removeFromInstanceGroup(RE_VertexArray *attrib);
829 
830 // DATA:
831  int myNumPoints;
832  int myNumVertices;
833  int myNumPrimitives;
834  RE_VertexMap *myVertexMap;
835  mutable int myAttribPIndex;
836  bool myUseVertexState;
837  bool myPurgeOnDelete;
838  int myVertexStateSerial;
839  UT_String myCacheName;
840  bool myUseTextures;
841  int myNumMaterialLayers;
842 
843  UT_ValArray<RE_VertexArray *> myAttributes;
844 
845  UT_ValArray<RE_VertexArray *> myTextureAttributes;
846  UT_ValArray<RE_Texture *> myBufferTextures;
847 
848  UT_ValArray<RE_VertexArray *> myKnownAttributes;
849  UT_ValArray<RE_VertexArray *> myStashedAttributes;
850 
851  UT_ValArray<re_Connectivity *> myConnectGroups;
852  UT_Array<re_InstanceGroup *> myInstanceGroups;
853 
854  RE_CacheTagHandle myCacheTagHandle;
855 
856  int myDebugDrawFlags;
857 
858  friend class re_InstanceGroup;
859 };
860 
861 inline void
863 {
864  myUseTextures = enable;
865 }
866 
867 inline void
869 {
870  myNumMaterialLayers = SYSclamp(num, 1, 32);
871 }
872 
873 inline RE_VertexArray *
875  const char *attrib_name,
876  RE_GPUType data_format,
877  int vectorsize,
878  RE_ArrayType array_type,
879  bool create_if_missing,
880  int array_size,
881  const RE_CacheVersion *cache_version,
883  const char *cache_prefix,
884  int capacity)
885 {
886  return fetchCachedAttrib(r, attrib_name, data_format, vectorsize,
887  array_type, 0, create_if_missing,
888  array_size, cache_version, usage,
889  cache_prefix, capacity);
890 }
891 
892 inline RE_VertexArray *
894  const char *attrib_name,
895  RE_GPUType data_format,
896  int vectorsize,
897  int inst_step,
898  int array_size,
899  bool create_if_missing,
900  const RE_CacheVersion *cache_version,
902  const char *cache_prefix,
903  int capacity)
904 {
905  return fetchCachedAttrib(r, attrib_name, data_format, vectorsize,
906  RE_ARRAY_INSTANCE, inst_step,
907  create_if_missing, array_size,
908  cache_version, usage, cache_prefix, capacity);
909 }
910 
911 inline RE_VertexArray *
913 {
914  return (id > RE_GENATTRIB_NONE) ? myKnownAttributes(id) : NULL;
915 }
916 
917 #endif
918 
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:874
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:862
GLuint const GLchar * name
Definition: glew.h:1814
#define RE_API
Definition: RE_API.h:10
GLuint index
Definition: glew.h:1814
void purgeOnDelete(bool purge=true)
Remove all buffers from the cache when this geometry object is deleted.
Definition: RE_Geometry.h:313
void setNumMaterialLayers(int num)
Clamp the number of layers to draw with a multi-layer material.
Definition: RE_Geometry.h:868
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
A collection of vertex arrays defining a geometry object. This class acts as a wrapper around multipl...
Definition: RE_Geometry.h:53
const RE_VertexMap * getVertexMap() const
Return a vertex map representing the layout locations of the attributes.
Definition: RE_Geometry.h:238
const GLdouble * v
Definition: glew.h:1391
GLuint shader
Definition: glew.h:1813
bool isCaching() const
Check if this geometry object is caching.
Definition: RE_Geometry.h:310
RE_GenericAttribID
Definition: RE_Types.h:328
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
std::string OIIO_API replace(string_view str, string_view pattern, string_view replacement, bool global=false)
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
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:622
GLuint GLuint GLsizei GLenum const void * indices
Definition: glew.h:1253
GLsizeiptr const void GLenum usage
Definition: glew.h:1681
UT_Vector3T< T > SYSclamp(const UT_Vector3T< T > &v, const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
Definition: UT_Vector3.h:836
RE_VertexArray * getAddressableAttributeByIndex(int index) const
Return the vertex arrayrepresenting addressable attribute 'i'.
Definition: RE_Geometry.h:286
GLuint GLsizei GLsizei * length
Definition: glew.h:1825
long long int64
Definition: SYS_Types.h:116
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:8011
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:291
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
GLuint start
Definition: glew.h:1253
GLsizei stride
Definition: glew.h:1523
const void * indirect
Definition: glew.h:2669
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:893
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 num
Definition: glew.h:2690
int getNumAddressableAttributes() const
Return the number of addressable attributes.
Definition: RE_Geometry.h:279
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
RE_ArrayType
Definition: RE_Types.h:317
RE_BufferUsageHint
Definition: RE_Types.h:276
GLenum array
Definition: glew.h:9066
const char * getCacheName() const
Return the base cache name.
Definition: RE_Geometry.h:307
Simple class for a mutli-integer cache tag.
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
GLboolean enable
Definition: glew.h:2745
GLboolean GLuint group
Definition: glew.h:2745
std::enable_if< internal::is_string< S >::value >::type print(std::basic_ostream< FMT_CHAR(S)> &os, const S &format_str, const Args &...args)
Definition: ostream.h:146