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