HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RV_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: RV_Geometry.h ( RV Library, C++)
7  *
8  * COMMENTS:
9  * High level class for wrapping geometry for drawing
10  */
11 
12 #ifndef RV_Geometry_h
13 #define RV_Geometry_h
14 
15 #include "RV_API.h"
16 
17 #include <RE/RE_CachedObject.h>
18 #include <SYS/SYS_Types.h>
19 #include <UT/UT_Array.h>
20 #include <UT/UT_Assert.h>
21 #include <UT/UT_ComputeGPU.h>
22 #include <UT/UT_Matrix4.h>
23 #include <UT/UT_StringHolder.h>
24 #include <UT/UT_StringMap.h>
25 #include <UT/UT_UniquePtr.h>
26 #include <UT/UT_ValArray.h>
27 #include <UT/UT_VectorTypes.h>
28 #include <UT/UT_WorkBuffer.h>
29 
30 #include <VE/VE_VK.h>
31 
32 #include "RV_Type.h"
33 #include "RV_TypePtrs.h"
34 #include "RV_VKBuffer.h"
35 
36 class RV_Render;
37 class RV_ShaderBlock;
38 class RV_ShaderProgram;
44 
45 typedef RV_VKBuffer RV_Buffer;
46 
48 {
50 };
51 
53 {
57 };
58 
60 {
61  switch (t)
62  {
74  default : break;
75  }
76 
77  UT_ASSERT_MSG(false, "RVgetVkPrimType Invalid RV_PrimType");
79 }
80 
82 {
83  switch (t)
84  {
85  case RV_GPU_UINT1: return UT_GPU_UINT1;
86  case RV_GPU_UINT4: return UT_GPU_UINT4;
87  case RV_GPU_UINT8: return UT_GPU_UINT8;
88  case RV_GPU_UINT16: return UT_GPU_UINT16;
89  case RV_GPU_UINT32: return UT_GPU_UINT32;
90 
91  case RV_GPU_INT8: return UT_GPU_INT8;
92  case RV_GPU_INT16: return UT_GPU_INT16;
93  case RV_GPU_INT32: return UT_GPU_INT32;
94 
95  case RV_GPU_FLOAT16: return UT_GPU_FLOAT16;
96  case RV_GPU_FLOAT24: return UT_GPU_FLOAT24;
97  case RV_GPU_FLOAT32: return UT_GPU_FLOAT32;
98  case RV_GPU_FLOAT64: return UT_GPU_FLOAT64;
99 
100  case RV_GPU_MATRIX2: return UT_GPU_MATRIX2;
101  case RV_GPU_MATRIX3: return UT_GPU_MATRIX3;
102  case RV_GPU_MATRIX4: return UT_GPU_MATRIX4;
103  default : break;
104  }
105 
106  UT_ASSERT(!"Unknown RV_GPUType.");
107  return UT_GPU_UINT8;
108 }
109 
111 {
112  switch (t)
113  {
114  case UT_GPU_UINT1: return RV_GPU_UINT1;
115  case UT_GPU_UINT4: return RV_GPU_UINT4;
116  case UT_GPU_UINT8: return RV_GPU_UINT8;
117  case UT_GPU_UINT16: return RV_GPU_UINT16;
118  case UT_GPU_UINT32: return RV_GPU_UINT32;
119 
120  case UT_GPU_INT8: return RV_GPU_INT8;
121  case UT_GPU_INT16: return RV_GPU_INT16;
122  case UT_GPU_INT32: return RV_GPU_INT32;
123 
124  case UT_GPU_FLOAT16: return RV_GPU_FLOAT16;
125  case UT_GPU_FLOAT24: return RV_GPU_FLOAT24;
126  case UT_GPU_FLOAT32: return RV_GPU_FLOAT32;
127  case UT_GPU_FLOAT64: return RV_GPU_FLOAT64;
128 
129  case UT_GPU_MATRIX2: return RV_GPU_MATRIX2;
130  case UT_GPU_MATRIX3: return RV_GPU_MATRIX3;
131  case UT_GPU_MATRIX4: return RV_GPU_MATRIX4;
132  default : break;
133  }
134 
135  UT_ASSERT(!"Unknown UT_GPUType.");
136  return RV_GPU_UNKNOWN;
137 }
138 
139 struct rv_DrawSetInfo;
140 struct rv_DrawInputInfo;
141 struct rv_Attribute;
142 struct rv_Connectivity;
143 struct rv_InstanceGroup;
144 
145 // TODO: use cache for multiple Drawing-Sets
146 //typedef UT_Map<RV_VKDescriptorSetInfo, rv_DrawSetInfo> rv_DrawSetCache;
147 
150 
151 /// @brief Object that represents drawable geometry.
152 /// This object holds vertex, instancing and index buffers for drawing.
153 /// The normal workflow is to:
154 /// - Set the number of points in the geometry with `setNumPoints()`
155 /// - Create all needed attribute definitions (P, N, uv, etc).
156 /// - Create all needed index buffer definitions or connect groups
157 /// - Call populateBuffers()
158 /// - Fetch each attribute with getAttribute() and fill it with
159 /// `RV_Buffer::uploadData()`
160 /// - Fetch each index buffer with getIndexBuffer() and fill it with
161 /// `RV_Buffer::uploadData()`
162 /// - Draw connect groups with any of the draw...() methods.
163 /// Draws must be done inside a begin/endRendering() block; all others must be
164 /// done outside.
166 {
167 public:
168  RV_Geometry();
169  ~RV_Geometry();
170 
171  // Setup and cleanup
172 
173  /// clear & free resources
174  void reset();
175  /// remove from VRAM; move to back in RAM (not implemented yet)
176  void unload();
177 
178  /// Memory use of all buffers for this geometry (in bytes) including RT
179  /// acceleration structures.
180  exint gpuMemoryUsage() const;
181 
182  /// Set name used for debugging
183  void setName(const UT_StringHolder &name) { myName = name; }
184  /// Get name used for debugging
185  const UT_StringHolder &getName() const { return myName; }
186 
187  /// Set number of points in geometry for vertex shader inputs
188  bool setNumPoints(exint num_points);
189  /// Number of points in geometry for vertex shader inputs
190  exint getNumPoints() const;
191 
192  /// Set number of per-vertex elements, accessed in fragment shader/
193  /// Must match the number of primitives drawn.
194  void setVertexBufferSize(exint vert_buffer_elements);
195  /// Number of per-primitive elements, accessed in fragment shader
196  exint getVertexBufferSize() const;
197 
198  /// Set number of per-primitive elements, accessed in fragment shader/
199  /// Must match the number of primitives drawn.
200  void setPrimitiveBufferSize(exint prim_buffer_elements);
201  /// Number of per-primitive elements, accessed in fragment shader
202  exint getPrimitiveBufferSize() const;
203 
204  // ---------------------------------------------------------------
205  // Methods for configuring the structure of the geometry
206 
207  /// @brief Create an attribute definition
208  /// Create an attribute definition for a vertex shader input
209  /// Capacity - optional buffer capacity in elements, in case of resizes
210  bool createAttribute(const UT_StringHolder &name,
211  RV_GPUType data_format, int vector_size,
212  exint capacity = -1, bool normalized = true,
213  bool allow_gpu_write = false);
214 
215  /// Creates a single value for a vertex shader input.
216  bool createConstant(const UT_StringHolder &name,
217  RV_GPUType data_format, int vector_size,
218  bool normalized = true);
219 
220  /// Create a vertex buffer for fragment shader indexing via vertex ID
221  bool createVertexBuffer(const UT_StringHolder &name,
222  RV_GPUType data_format, int vector_size,
223  exint capacity = -1, bool normalized = true,
224  bool allow_gpu_write = false);
225 
226  /// Create a buffer view for fragment shader indexing via gl_PrimitiveID
227  bool createPrimitiveBuffer(const UT_StringHolder &name,
228  RV_GPUType data_format, int vector_size,
229  bool normalized = true);
230 
231  /// Create a random-access attribute for instancing and other random access
232  /// Uses a buffer view.
233  bool createBufferAttribute(const UT_StringHolder &name,
234  RV_GPUType data_format, int vector_size,
235  exint buffer_size, bool normalized = true);
236 
237  /// Create an attribute that is allocated immediately
238  /// separate from those allocated during 'populateBuffers'
239  RV_Buffer* createAllocatedAttribute(RV_Render* r,
240  const UT_StringHolder &name,
241  RV_GPUType data_format, int vector_size,
243  exint capacity = -1,
244  bool normalized = true);
245 
246  /// @brief Fetch a buffer for an attribute.
247  /// Return the RV buffer for attribute `name`. Will not exist until
248  /// `populateBuffers()` is called.
249  RV_Buffer* getAttribute(const UT_StringHolder &name) const;
250  /// Tag the attribute with version information.
251  void setAttributeVersion(const UT_StringHolder &name,
252  const RE_CacheVersion &ver);
253  /// Set the attribute type (point, primitive, constant, random).
254  void setAttributeUsage(const UT_StringHolder &name,
255  RV_AttributeType ver);
256 
257  /// Set the attribute's original format, used for display
258  void setAttributeDisplayFormat(const UT_StringHolder &name,
259  RV_GPUType out_type,
260  int out_vec_size) const;
261 
262  /// Record a vertex shader input's constant scalar value.
263  void setAttributeConstValue(const UT_StringHolder &name,
264  fpreal64 val);
265  /// Record a vertex shader input's constant vector value.
266  void setAttributeConstVecValue(const UT_StringHolder &name,
267  const UT_Vector4F &val);
268 
269  /// Disables all attributes that are marked as stashable, and clears
270  /// attributes that were previously stashed. Attributes can be marked
271  /// as stashable and kept active by calling `unstashAttribute()` after
272  /// calling `stashAttributes()`
273  /// Should only be called by `GR_GeoRenderVK::fillGeoAttribs()`
274  void stashAttributes();
275 
276  /// Marks an attribute as active and stashable
277  bool unstashAttribute(const UT_StringHolder &name);
278  bool unstashInstanceAttrib(int group_index,
279  const UT_StringHolder &name);
280  /// Marks an attribute as not stashable
281  void disableAttribStashing(const UT_StringHolder &name);
282 
283  /// Remove attribute and free its resource
284  bool freeAttribute(RV_Render* r, const UT_StringHolder &name);
285 
286  /// @brief Get an attribute's usage
287  /// Get the type of an attribute (point, prim, const, random, null). May be
288  /// null if the attribute was freed.
290  RV_AttributeType& out_type) const;
291 
292  /// Get the format of an attribute (return false if it does not exist).
293  bool getAttributeFormat(const UT_StringHolder &name,
294  RV_GPUType& out_type,
295  int& out_vec_size) const;
296 
297  /// @brief Get the length of an attribute
298  /// Get the length, in elements (not bytes) of an attribute
299  /// (return false if it does not exist).
300  bool getAttributeLength(const UT_StringHolder &name,
301  exint& out_len) const;
302  /// Get the version of an attribute (return false if it does not exist).
303  bool getAttributeVersion(const UT_StringHolder &name,
304  RE_CacheVersion& out_ver) const;
305 
306  /// @brief Get an attribute's usage
307  /// Get the version of an attribute (return false if it does not exist).
308  /// Will always return the usage it was defined with, even if freed.
309  bool getAttributeUsage(const UT_StringHolder &name,
310  RV_AttributeType& out_ver) const;
311 
312  /// Get the display format of an attribute (return false if it does not exist).
313  /// May be different from the actual Vulkan Buffer's data format, if an
314  /// implicit reformatting occured on the data before upload
315  bool getAttributeDisplayFormat(const UT_StringHolder &name,
316  RV_GPUType& out_type,
317  int& out_vec_size) const;
318 
319  /// Get the constant value for the vertex input 'name', after set as scalar
320  fpreal64 getAttributeConstValue(const UT_StringHolder &name) const;
321 
322  /// Get the constant value for the vertex input 'name', after set as vector
323  const UT_Vector4F&getAttributeConstVecValue(const UT_StringHolder &name) const;
324 
325  /// True if the attribute exists in the geometry.
326  bool hasAttribute(const UT_StringHolder &name,
327  int instance_group = -1) const;
328 
329  // Instancing support ----------------------------------------------
330 
331  // subsets of instances (for materials)
332 
333  /// Create a instance group for instanced drawing
334  bool createInstanceGroup(int group_index);
335  /// Remove an instance group
336  void removeInstanceGroup(int group_index);
337  /// Return true if the instance group exists
338  bool hasInstanceGroup(int group_index) const;
339  /// Return the maximum index of all instance groups.
340  int getMaxInstanceGroup() const;
341 
342  /// Set the numbers of instances drawn in a group
343  void setInstanceGroupCount(int instance_group, int count);
344  /// Fetch the numbers of instances drawn in a group
345  int getInstanceGroupCount(int instance_group) const;
346  /// Fetch the numbers of instanced transforms in a group, ignoring index lists
347  int getInstanceGroupTransformCount(int instance_group) const;
348 
349  /// Set the group to draw all transforms, ignoring existing index list
350  void setInstanceGroupForceFullDraw(int instance_group, bool enable);
351  /// Fetch whether group was set to force full draws
352  bool getInstanceGroupForceFullDraw(int instance_group) const;
353 
354  /// Select a subset of instance indices for an instance group (eg, culling)
355  void setInstanceGroupIndexList(int group_index,
356  const UT_IntArray &indices,
357  int max_capacity = -1);
358  /// Draw one instance with a specific index
359  void setInstanceGroupConstIndex(int instance_group,
360  int instance_to_draw);
361  /// Draw all instances
362  void setInstanceGroupDrawEverything(int instance_group);
363  /// Draw none of the instances
364  void setInstanceGroupDrawNothing(int instance_group);
365 
366  // Transforms for instances
367 
368  /// Single transform for all instances (or additional transform if
369  /// remove_instanced_xform is false)
370  void setConstInstanceGroupTransform(int instance_group,
371  const UT_Matrix4F &xform,
372  bool remove_instanced_xform);
373 
374  /// set instance group to use the same const transform as another
375  void copyConstInstanceGroupTransform(int instance_group, int src_group);
376 
377  /// @brief Create an per-instance attribute
378  /// Reserve an buffer for per-instance data (call populateBuffers() to
379  /// create the RV_Buffer for it).
380  bool setInstanceGroupAttrib(int instance_group,
381  const UT_StringHolder &name,
382  RV_GPUType data_type,
383  int vector_size,
384  int instance_step,
385  int num_instances,
386  exint capacity = -1,
387  bool normalized = true);
388 
389  /// Create per-instance attribute that can be shared by multiple RV_Geometrys
390  bool createSharedInstanceGroupAttrib(int instance_group,
391  const UT_StringHolder &name,
392  RV_GPUType data_type,
393  int vector_size,
394  int instance_step,
395  int num_instances,
396  exint capacity = -1,
397  bool normalized = true);
398 
399  /// Assign a buffer for the per-instance shared attribute
400  bool assignSharedInstanceGroupAttrib(int instance_group,
401  const UT_StringHolder &name,
403 
404  /// Remove per-instance attribute and free its resource
405  bool freeInstanceGroupAttrib(int instance_group,
406  const UT_StringHolder &name);
407 
408  /// Helper to allocate a shared per-instance buffer
409  static RV_VKBufferPtr allocateSharedInstanceBuffer(
410  RV_Render* r,
411  RV_GPUType data_type,
412  int vector_size,
413  exint length,
414  bool normalized = true,
415  const char* name = nullptr);
416 
417  static RV_VKBufferPtr allocateSharedCPUMappedInstanceBuffer(
418  RV_Render* r,
419  RV_GPUType data_type,
420  int vector_size,
421  exint length,
422  bool normalized = true,
423  bool cpu_mapped = true,
424  const char* name = nullptr);
425 
426  /// Fetch the buffer backing an per-instance attribute, usually to fill it
427  RV_Buffer * getInstanceGroupAttrib(int instance_group,
428  const UT_StringHolder &name) const;
429  /// @brief Get the length of an attribute
430  /// Get the length, in elements (not bytes) of an attribute
431  /// (return false if it does not exist).
432  bool getInstanceAttribLength(int instance_group,
433  const UT_StringHolder &name,
434  exint& out_len) const;
435 
436  /// @brief Get the instance stride
437  /// (return false if it does not exist).
438  bool getInstanceAttribStride(int instance_group,
439  const UT_StringHolder &name,
440  exint& out_stride) const;
441 
442  /// Fetch the per-instance attribute version (to avoid refilling)
443  bool getInstanceAttribVersion(int instance_group,
444  const UT_StringHolder &name,
445  RE_CacheVersion& out_ver) const;
446  /// Set the per-instance attribute version (to avoid refilling)
447  void setInstanceAttribVersion(int instance_group,
448  const UT_StringHolder& name,
449  const RE_CacheVersion& ver);
450 
451  // ----------------------------------------------------------------------
452  // Connectivity
453 
454  /// Connect all points to form prims of `prim` type
455  void connectAllPrims(int connect_index,
456  RV_PrimType prim,
457  int patch_size = 0);
458  /// Connect a range of points to form prims of `prim` type
459  void connectSomePrims(int connect_index,
460  RV_PrimType prim,
461  int start,
462  int length,
463  unsigned int stride = 0,
464  int patch_size = 0);
465  /// Connect points form prims `prim` type from a list of `num_elements`
466  /// point indices. The index buffer must be populated after a
467  /// `populateBuffers()` call with `getIndexBuffer()->uploadData()`.
468  void connectIndexedPrims(int connect_index,
469  RV_PrimType prim,
470  int num_elements,
471  bool need_prim_info = false,
472  int patch_size = 0);
473  /// Connect a range of vertices to form prims of `prim` type
474  void connectSomeVerts(int connect_index,
475  RV_PrimType prim,
476  int start,
477  int length,
478  unsigned int stride = 0,
479  int patch_size = 0);
480 
481  void connectIndexedVerts(int connect_index,
482  RV_PrimType prim,
483  int num_elements,
484  bool need_prim_info = false,
485  int patch_size = 0);
486 
487  /// Set the primitive to GPU primitive ratio for acessing primitive
488  /// attributes. For example, if all polygons are quads, the divisor would be
489  /// 2 as the GPU creates 2 triangles for each quad.
490  void setConnectDivisor(int connect_index, int divisor,
491  int base_prim=0);
492  /// Generate a primitive info buffer for accessing primtive attribute data
493  /// if the polygon vertex counts vary (use getPrimVertexBuffer() after
494  // calling populateBuffers() to fill it).
495  void setConnectUsePrimInfo(int connect_index, int base_prim= 0);
496 
497  // Override the parameters used for the draw call in the connect group,
498  // without modifying the connect group
499  void setDrawModeOverride(int connect_index,
500  RV_PrimType prim_type,
501  exint vert_count);
502 
503  // disables any active draw mode overrides for the given connect group
504  void resetDrawModeOverride(int connect_index);
505 
506  /// Set a PrimInfo buffer to be shared across connect groups with same
507  /// primitive type. Attribute must already exist in geometry.
508  void setPrimitiveInfoAttrib(RV_TopologyClass prim_type,
509  const UT_StringHolder &name);
510  /// return the number of primitives that are rendered for a connect group
511  int getNumRenderPrims(int connect_index) const;
512 
513  /// return the type of primitives that are rendered for a connect group
514  RV_PrimType getRenderPrimType(int connect_index) const;
515 
516  /// return the number of vertices that are rendered for a connect group
517  int getLength(int connect_index) const;
518 
519  /// Fetch the index buffer to fill (after populateBuffers())
520  RV_Buffer *getIndexBuffer(int connect_index) const;
521  /// Fetch the primitive buffer to fill (after populateBuffers())
522  RV_Buffer *getPrimBuffer(int connect_index) const;
523  /// Fetch the primitive/vertex buffer to fill (after populateBuffers())
524  RV_Buffer *getPrimVertexBuffer(int connect_index) const;
525 
526  // True if the vertex shader is run at vertex frequency (False if at point)
527  bool isVertexFrequency(int connect_index) const;
528 
529  /// Return true if the connect group exists
530  bool hasConnectGroup(int connect_index) const;
531  /// Return true if the connect group isn't empty
532  bool hasNonEmptyConnectGroup(int connect_index) const;
533  /// Return the maximum connect group index
534  int getMaxConnectGroup() const;
535 
536  /// Remove a connect group
537  bool removeConnectedPrims(int connect_index);
538  /// Remove all connect groups.
539  void resetConnectedPrims(); // remove all
540 
541  // -------------------------------------------------------------
542  /// Populates all RV_Buffers declared above which haven't been populated yet.
543  bool populateBuffers(RV_Render *r);
544 
545  // --------------------------------------------------------------
546  // Drawing
547 
548  /// Setup Uniform buffers and Descriptor Set to be used by current shader
549  void prepareForDraw(RV_Render *r,
550  int connect_index,
551  const RV_OverrideList *overrides = nullptr);
552  /// Setup Uniform buffers and Descriptor Set to be used by current shader,
553  /// when drawing with instances
554  void prepareForDrawInstance( RV_Render* r,
555  int connect_index,
556  int instance_group,
557  const RV_OverrideList *overrides = nullptr);
558 
559  /// draw a single connection group
560  /// draw_offset and draw_size can specifiy a vertex range in the connect
561  void draw(
562  RV_Render *r,
563  int connect_index,
564  const RV_OverrideList *overrides = nullptr,
565  exint draw_offset = -1, exint draw_size = -1);
566 
567  /// draw a single connection group using with instancing, using the indices
568  /// in instance_group
569  /// draw_offset and draw_size can specifiy a vertex range in the connect
570  void drawInstanceGroup(
571  RV_Render *r,
572  int connect_index,
573  int instance_group,
574  const RV_OverrideList *overrides = nullptr,
575  exint draw_offset = -1, exint draw_size = -1);
576 
577  /// draw multiple connection groups (really just a loop over draw()).
578  void drawRange(
579  RV_Render *r,
580  int connect_index_start,
581  int num_connect_indexs,
582  const RV_OverrideList *overrides = nullptr);
583 
584  /// draw instanced over multiple connection groups.
585  void drawInstanceGroupRange(
586  RV_Render *r,
587  int connect_start,
588  int num_connect,
589  int instance_group,
590  const RV_OverrideList *overrides = nullptr);
591 
592  /// draw indirectly from a buffer
593  void drawIndirect(
594  RV_Render *r,
595  int connect_index,
596  RV_Buffer &indirect_buffer);
597 
598  /// dispatch a compute shader for a single connect group
599  void dispatch(RV_Render *r,
600  int connect_index,
601  int instance_group,
602  const RV_OverrideList *overrides = nullptr);
603 
604  /// dispatch compute shader for multiple connect groups
605  void dispatchRange(RV_Render *r,
606  int connect_index,
607  int num_connect,
608  int instance_group,
609  const RV_OverrideList *overrides = nullptr);
610 
611  static void initResources(RV_Instance* inst);
612  static void cleanupResources();
613 
614  // --------------------------------------------------------------
615  // Ray tracing
616  void updateAccelerationStructure(
617  RV_Render *r,
618  int tag,
619  int light_id,
620  int surface_connect_index,
621  const UT_Matrix4F &obj_mat);
622 
623  RV_VKAccelerationStructure* getAccelerationStructure();
624 
625  // manually force acceleration structure rebuild
626  void dirtyAccelerationStructure() { myIsAccelStructDirty = true; }
627 
628  // -------------------------------------------------------------------
629  // DEBUG
630 
631  /// @brief Debug output
632  /// Debug methd to print information about all attributes, connect groups,
633  /// and instance groups
634  void print() const;
635  // TODO: setDebugDraw() to print shader state during draw
636 
637 private:
638 
639  // ------------------------------------------------------------------
640  // CONNECTIVITY
641  rv_Connectivity* getConnect(int group_idx, bool do_create);
642 
643  // ------------------------------------------------------------------
644  // INSTANCE
645  static void getInstanceAttribName(UT_WorkBuffer &out_name,
646  const char *base_name,
647  int instance_group);
648 
649  int getTransformAttribCount(const rv_Attribute* attr) const;
650 
651  // ------------------------------------------------------------------
652  // ATTRIBUTES
653  const rv_Attribute* findAttribute(const UT_StringRef& name,
654  const RV_OverrideList *overrides,
655  int instance_group,
656  int* out_idx = nullptr) const;
657 
658  bool privCreateAttribute( const UT_StringHolder& name,
659  RV_GPUType data_format,
660  int vector_size,
661  bool normalize,
663  exint capacity = -1,
664  int inst_group = -1,
665  bool attr_owned = false,
666  bool allow_gpu_write = false);
667  bool privFreeAttrib(RV_Render* r, rv_Attribute &attr);
668 
669  // ------------------------------------------------------------------
670  // BUFFERS
671  RV_Buffer *allocateAttributeBuffer(
672  RV_Render *r,
673  rv_Attribute &attr,
674  UT_Array<RV_BufferPtr> *opt_old_buffers = nullptr);
675  bool allocateConnectBuffers(RV_Render* r, rv_Connectivity& c,
676  UT_Array<RV_BufferPtr>* opt_old_buffers);
677  bool allocateInstIndexBuffer(RV_Render* r, rv_InstanceGroup& g,
678  UT_Array<RV_BufferPtr>* opt_old_buffers);
679 
680  RV_BufferPtr allocOrReuseBuffer( RV_Render *r, rv_Attribute &attr,
681  exint length, VkBufferUsageFlags vk_usage,
682  UT_Array<RV_BufferPtr> *old_buffers);
683 
684  void resetAttribBuffers(RV_Render* r);
685  void resetConnectBuffers(RV_Render* r);
686  void resetCachedSets(RV_Render* r);
687 
688  bool privPrepareDraw(
689  RV_Render* r,
690  const rv_Connectivity *connect,
691  int instance_group_idx,
692  const RV_OverrideList* overrides
693  );
694 
695  void privDraw(
696  RV_Render *r,
697  const rv_Connectivity *connect,
698  int instance_group_idx,
699  const RV_OverrideList *overrides,
700  RV_Buffer* indirect_buf,
701  exint draw_offset = -1,
702  exint draw_length = -1
703  );
704 
705  void privDispatch(
706  RV_Render *r,
707  const rv_Connectivity *connect,
708  int instance_group_idx,
709  const RV_OverrideList *overrides,
710  RV_Buffer *indirect_buf
711  );
712 
713  void fillInstanceInfoBlock(RV_Render *r, int instance_group_idx, RV_ShaderBlock* block);
714  void fillGeometryInfoBlock(RV_Render *r, const rv_Connectivity* connect, RV_ShaderBlock* geo);
715 
716  // ------------------------------------------------------------------
717  // DATA
718  RV_Render* myR = nullptr;
719  UT_StringHolder myName;
720 
721  uint32_t myNumPoints = 0;
722  uint32_t myVertBufferSize = 0;
723  uint32_t myPrimBufferSize = 0;
724 
725  bool myAttributesValid = false;
726 
727  // TODO: Make sure nothing name-to-idx mapping is stable
728  // (i.e. attrib names can't be removed)
729  UT_StringMap<int> myAttributeTable;
730  UT_Array<int> myDefaultAttribTable;
731  UT_Array<int> myPrimInfoAttribTable;
732  UT_Array<rv_Attribute> myAttributeList;
733 
734  // own directly so helper structs can be passed around more easily
735  UT_Array<RV_BufferPtr> myOwnedAttribBuffers;
736  UT_Array<RV_BufferPtr> myOwnedConnectBuffers;
737  UT_Array<RV_BufferPtr> myOwnedFallbackBuffers;
738  UT_Array<UT_UniquePtr<RV_ShaderBlock>> myOwnedGeoBuffers;
739  UT_Array<UT_UniquePtr<RV_ShaderBlock>> myOwnedInstBuffers;
740 
741  // TODO: allocate buffers from single block of memory
742  //UT_UniquePtr<RV_VKMemory> myMemBlock;
743 
744  UT_Array<rv_Connectivity> myConnectivityGroups;
745 
746  UT_Array<rv_InstanceGroupPtr> myInstanceGroups;
747 
748  struct rv_DrawSetInfo
749  {
750  // track the override list used
751  RV_OverrideList myOverrideList;
752  int myConnectGroupIdx;
753  int myInstanceGroupIdx;
754 
755  bool myIsValid = false;
756 
757  UT_UniquePtr<RV_ShaderVariableSet> myDescriptorSet;
758  UT_UniquePtr<RV_ShaderBlock> myUniformBlock;
759 
760  // TODO: move into connect group
761  UT_UniquePtr<RV_ShaderBlock> myGeometryBlock;
762 
763  int myInstTransformUniformIdx = -1;
764  int myInstNestLevelUniformIdx = -1;
765 
766  // TODO: keep a list of the [attrib ID, binding ID] pairs
767  // and check if they are up-to-date per-frame
768  // TODO: requires RV_Geo and RV_ShaderVariableSet
769  // be setup to refer to attribs by consistent
770  // idx/binding num
771  // currently invalidating any time the interface
772  // may have changed
773  };
774 
775  // Set Used for drawing
776  // cached by (connect group, instance group, set id)
777  typedef UT_Map<std::tuple<int, int, int>, rv_DrawSetInfo> DrawSetCache;
778  DrawSetCache myDrawSet;
779 
780  // Get the set that is compatible with the passed in shader
781  // creating it if necessary
782  rv_DrawSetInfo* getDrawSet(
783  RV_Render* r,
785  const rv_Connectivity *connect,
786  int instance_group_idx,
787  const RV_OverrideList* overrides);
788 
789  // Prepare a set, making sure all attributes are attached and
790  // up-to-date
791  bool prepareDrawSet(
792  RV_Render* r,
793  rv_DrawSetInfo& set_info,
794  const rv_Connectivity *connect,
795  int instance_group_idx,
796  const RV_OverrideList* overrides
797  );
798 
799  bool bindInputAttribute(
800  RV_Render* r,
801  RV_VKPipelineInputInfo& pipe_input,
802  const rv_Connectivity *connect,
803  const rv_Attribute *attrib,
804  bool instance,
805  int location,
807  int vec_size);
808  //rv_DrawSetCache myDrawSets;
809 
810  // Zero-valued constant buffers to fill attribs we don't have values for
811  // TODO: use nullDescriptor bindings if VK_EXT_robustness2 is available
812  void populateFallbackAttribs(RV_Render* r);
813  void resetFallbackAttribs(RV_Render* r);
814  rv_Attribute* getFallbackVertAttrib(RV_GPUType type);
815  rv_Attribute* getFallbackTexBufAttrib(RV_UniformType type);
816  UT_UniquePtr<rv_Attribute> myDefaultVertexAttrib;
817  UT_UniquePtr<rv_Attribute> myDefaultIntVertexAttrib;
818  UT_UniquePtr<rv_Attribute> myDefaultUIntVertexAttrib;
819  UT_UniquePtr<rv_Attribute> myDefaultSamplerAttrib;
820  UT_UniquePtr<rv_Attribute> myDefaultIntSamplerAttrib;
821  UT_UniquePtr<rv_Attribute> myDefaultUIntSamplerAttrib;
822  UT_UniquePtr<rv_Attribute> myDefaultIndexSamplerAttrib;
823  UT_UniquePtr<rv_Attribute> myDefaultIndexVertexAttrib;
824 
825  // Ray tracing
826  RV_VKAccelerationStructurePtr myAccelerationStructure;
827 
828  UT_Matrix4F myLastObjTransform = UT_Matrix4F::getIdentityMatrix();
829  bool myIsAccelStructDirty = true;
830  bool myIsAccelStructInstDirty = true;
831 };
832 
833 #endif
A collection of Vulkan UBO, SSBO, and Image shader bindings (descriptor set)
RV_ConnectionType
Definition: RV_Geometry.h:52
VkPrimitiveTopology
Definition: vulkan_core.h:1957
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
VkFlags VkBufferUsageFlags
Definition: vulkan_core.h:2515
Unsorted map container.
Definition: UT_Map.h:109
VkPrimitiveTopology RVgetVkPrimType(RV_PrimType t)
Definition: RV_Geometry.h:59
UT_UniquePtr< RV_VKBuffer > RV_VKBufferPtr
Definition: RV_TypePtrs.h:63
GLuint start
Definition: glcorearb.h:475
GLboolean GLboolean g
Definition: glcorearb.h:1222
int64 exint
Definition: SYS_Types.h:125
Object that represents drawable geometry. This object holds vertex, instancing and index buffers for ...
Definition: RV_Geometry.h:165
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
RV_GPUType RVconvertFromGPUType(UT_GPUType t)
Definition: RV_Geometry.h:110
GLuint buffer
Definition: glcorearb.h:660
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
double fpreal64
Definition: SYS_Types.h:201
#define UT_ASSERT_MSG(ZZ,...)
Definition: UT_Assert.h:159
UT_GPUType
Definition: UT_ComputeGPU.h:42
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
GLboolean reset
Definition: glad.h:5138
UT_UniquePtr< RV_VKAccelerationStructure > RV_VKAccelerationStructurePtr
Definition: RV_TypePtrs.h:84
RV_AttributeType
Definition: RV_Type.h:99
static const UT_Matrix4T< fpreal32 > & getIdentityMatrix()
RV_UniformType
Definition: RV_Type.h:332
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:872
#define RV_API
Definition: RV_API.h:10
GLint location
Definition: glcorearb.h:805
RV_GenericAttribID
Definition: RV_Geometry.h:47
GLuint const GLchar * name
Definition: glcorearb.h:786
GLint GLenum GLboolean normalized
Definition: glcorearb.h:872
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:44
GLdouble t
Definition: glad.h:2397
UT_GPUType RVconvertToUTGPUType(RV_GPUType t)
Definition: RV_Geometry.h:81
RV_PrimType
Definition: RV_Type.h:424
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:664
UT_UniquePtr< rv_InstanceGroup > rv_InstanceGroupPtr
Definition: RV_Geometry.h:143
RV_TopologyClass
Definition: RV_Type.h:451
RV_GPUType
Definition: RV_Type.h:40
void dirtyAccelerationStructure()
Definition: RV_Geometry.h:626
GLuint GLfloat * val
Definition: glcorearb.h:1608
FMT_CONSTEXPR basic_fp< F > normalize(basic_fp< F > value)
Definition: format.h:1701
GR_API RE_GPUType getAttributeType(RE_Render *r, const char *attrib_name, int &vsize, const GT_DataArrayHandle &pa, const GR_DisplayOption &opts, RE_ArrayType array_type)
const UT_StringHolder & getName() const
Get name used for debugging.
Definition: RV_Geometry.h:185
Simple class for a mutli-integer cache tag.
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
RV_VKBuffer RV_Buffer
Definition: RV_Geometry.h:43
GLboolean r
Definition: glcorearb.h:1222
UT_UniquePtr< RV_Buffer > RV_BufferPtr
Definition: RV_Geometry.h:149
A vulkan buffer object.
Definition: RV_VKBuffer.h:81
GLuint divisor
Definition: glcorearb.h:1670
void setName(const UT_StringHolder &name)
Set name used for debugging.
Definition: RV_Geometry.h:183
FMT_INLINE void print(format_string< T...> fmt, T &&...args)
Definition: core.h:2903
GLint GLsizei count
Definition: glcorearb.h:405