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