HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GR_GeoRenderVK.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: GR_GeoRenderVK.h ( GR Library, C++)
7  *
8  * COMMENTS:
9  * Base class for all drawing methds that Houdini is natively capable of
10  * rendering with Vulkan.
11  */
12 
13 #ifndef GR_GeoRenderVK_h
14 #define GR_GeoRenderVK_h
15 
16 #include "GR_API.h"
17 
18 #include "GR_GeoRender.h"
19 
20 #include <GT/GT_Handles.h>
21 
22 #include <RV/RV_Type.h>
23 #include <RV/RV_Geometry.h> // for `RV_OverrideList`
24 
25 #include <UT/UT_Array.h>
26 #include <UT/UT_BoundingBox.h>
27 #include <UT/UT_UniquePtr.h>
28 #include <UT/UT_Options.h>
29 #include <UT/UT_UniquePtr.h>
30 
31 #include "GR_DecorationRender.h"
32 
33 class RV_Geometry;
34 class RV_Render;
36 class RV_ShaderBlock;
38 class RV_ShaderAtlas;
39 class RV_VKBuffer;
40 class RV_VKImage;
42 
43 class GR_InstanceBuffer;
44 class GR_Uniforms;
46 class gr_AttribInfoVK;
47 class gr_AttribCacheVK;
49 
50 // Multithreaded filling of attribute arrays
51 // TODO: replace with function pointer?
52 // or override?
54 {
67 };
68 
70 {
71 public:
72  GR_GeoRenderVK(GR_Primitive *parent_prim);
73  ~GR_GeoRenderVK() override;
74 
75  virtual bool supportsRayTrace() const
76  {
77  return false;
78  }
79 
80  RV_Geometry* createGeometryVK(exint num_points);
81 
83  {
84  return myGeometryVk.get();
85  }
86 
87  static bool initShaders(RV_Instance *inst);
88  static void cleanupShaders();
89 
90  virtual void getVkDecorConnectGroup(RV_PrimType render_prim,
91  bool is_uv_view,
92  int &out_connect_start,
93  int &out_connect_num) {} // = 0;
94 
95  static void fillGeoAttribs( RV_Render *r,
96  const GT_Primitive *prim,
97  const GR_UpdateParms&p,
98  RV_Geometry *geo,
99  GR_FillAttributesVK *attrib_fill,
100  bool needs_normal,
101  bool needs_uv,
102  bool needs_tangent,
103  bool need_prim_vert_info,
104  const GT_DataArrayHandle &point_normals,
105  GR_BasePrimType packed_type,
106  bool build_point_id,
107  bool build_prim_id,
108  GR_GeoRenderVK *geo_ren,
109  bool *has_detail_prim_id,
110  int *detail_prim_id,
111  int *num_bone_deform);
112 
113  void fillAttributesVK( RV_Render *r,
114  const GT_Primitive *prim,
115  const GR_UpdateParms &p,
116  RV_Geometry *geo,
117  GR_FillAttributesVK *attrib_fill,
118  bool needs_nml,
119  bool needs_uv,
120  bool needs_tangent,
121  bool need_prim_vert_info,
122  const GT_DataArrayHandle &point_norm);
123 
124  void buildPointID( RV_Render *r,
125  const GR_DisplayOption &dopts,
126  RV_Geometry *rv_geo,
127  GR_FillAttributesVK *attrib_fill,
128  const GT_Primitive &prim,
129  const GU_ConstDetailHandle &geo,
130  const RE_CacheVersion &ver);
131 
132  bool buildInstanceObjectAttrib( RV_Render *r,
133  const GT_PrimitiveHandle &h,
134  const GR_UpdateParms &p,
135  RV_Geometry *geo,
136  GR_FillAttributesVK *attrib_fill,
137  const UT_Matrix4DArray *instance_trans,
138  const RE_CacheVersion &instance_version,
139  int instance_group,
140  UT_Matrix4D *out_transform);
141 
142  static void addAttribArray( RV_Render *r,
143  RV_Geometry *geo,
144  GR_FillAttributesVK *attrib_fill,
145  const char *attrib_name,
146  RV_AttributeType array_type,
147  const GT_DataArrayHandle &dh,
148  const GR_DisplayOption &opts,
149  const RE_CacheVersion &geo_ver,
150  const gr_FillType &fill_type,
151  const GT_Owner original_type = GT_OWNER_INVALID,
152  bool *is_ce_attrib = nullptr);
153  static void addAttribArray( RV_Render *r,
154  RV_Geometry *geo,
155  GR_FillAttributesVK *attrib_fill,
156  const char *attrib_name,
157  RV_AttributeType array_type,
158  const GT_DataArrayHandle &dh,
159  const GT_DataArrayHandle &dhn,
160  const GT_DataArrayHandle &dhv,
161  const GT_DataArrayHandle &dhvid,
162  const GR_DisplayOption &opts,
163  const RE_CacheVersion &geo_ver,
164  const gr_FillType &fill_type,
165  const GT_Owner original_type = GT_OWNER_INVALID,
166  bool *is_ce_attrib = nullptr);
167 
168  void addExternalAttribArray( RV_Render *r,
169  RV_Geometry *geo,
170  GR_FillAttributesVK *attrib_fill,
171  const char *attrib_name,
172  RV_AttributeType array_type,
173  const GT_DataArrayHandle &dh,
174  const GR_DisplayOption &opts,
175  bool fill_immediately);
176 
177  void addInstAttribArray( RV_Render *r,
178  RV_Geometry *geo,
179  GR_FillAttributesVK *attrib_fill,
180  const char *attrib_name,
181  int inst_group,
182  RV_AttributeType array_type,
183  const GT_DataArrayHandle &dh,
184  const GR_DisplayOption &opts,
185  const RE_CacheVersion &geo_ver,
186  const gr_FillType &fill_type);
187 
188  void cleanupGeoOverride( RV_Render *r,
189  const GR_DisplayOption *opt,
190  RV_Geometry *geo);
191 
192  void setupGeoOverride( RV_Render *r,
193  const GR_DisplayOption *opt,
194  RV_Geometry *geo,
196  override_list);
197 
198  void setupGeoOverride( RV_Render *r,
199  const GR_DisplayOption *opt,
200  RV_Geometry *geo,
202  override_list);
203 
204  // Called by wire primitives (curves and points) to add a Cd override
205  // if the Geometry is supposed to have a special color
206  void updateWireColor(RV_Render *r,
207  GR_Uniforms *u,
208  RV_Geometry *geo,
209  const GR_DisplayOption *opt,
210  bool allow_color_override,
211  RV_OverrideList& overrides) const;
212 
213  void updateWireOverColor(RV_Render *r,
214  GR_Uniforms *u,
215  RV_Geometry *geo,
216  const GR_DisplayOption *opt,
217  bool allow_color_override,
218  RV_OverrideList& overrides) const;
219 
220  GR_FillAttributesVKPtr createFillTask( RV_Render *r,
221  GR_GeoRenderVK *gr,
222  const GT_Primitive *prim,
223  const GU_Detail *geo,
224  const GR_DisplayOption *opts,
225  int num_points);
226  void runFillTask( RV_Render *r,
227  RV_Geometry *geo,
228  GR_FillAttributesVK *attrib_fill
229  );
230 
231  void updateInstanceGroupBuffer(const GR_InstanceBuffer *buffer,
232  int instance_group);
233 
234  void gpuMemoryUsage(exint &used_bytes,
235  exint &unused_bytes,
236  exint &pinned_bytes) override;
237 
238  // Optionally overriden by geometries that should be traced against
239  virtual void updateAccelerationStructure(RV_Render *r,
240  int tag,
241  int light_id,
242  const UT_Matrix4F &obj_mat);
243 
244  virtual RV_VKAccelerationStructure* getAccelerationStructure();
245 
246  void prepareForRayTrace(RV_Render *r,
247  int tag,
248  int light_id,
249  const UT_Matrix4F &obj_mat);
250 
251 protected:
252 
253  bool initTransformAndPassInfo(RV_Render *r,
254  GR_Uniforms *u,
256 
258  const GR_UpdateParms &p,
259  RV_Geometry *geo,
260  GR_FillAttributesVK *attrib_fill,
261  const GT_PrimitiveHandle &primh,
262  const GT_DataArrayHandle &vertex_list);
263 
264  // Generate normals with compute shaders. The geometry must have the given
265  // connect group, as well as P, N, and PointIndex attributes.
266  // P and N must each have a vector size of 4.
267  bool generateNormalsGPU(RV_Render *r,
268  RV_Geometry *geo,
269  int connect_index,
270  const GR_DisplayOption &opts);
271 
272  void clearScratchData();
273 
274  // --------------------------------------------------
275  // gr_SetCache
276  //
277  // Sets keyed by their pipeline
278  // TOOD: not enforced, but ideally should be a small number of sets
279  // e.g. sets with same/similar layout but used with pipelines that
280  // have different usage state (e.g. Vertex-Only, or Frag-and-Vertex)
281  // TODO: could also have a mechanism in RV_ShaderAtlas to enforce that all
282  // sets with the same members and per-set locations also have the same
283  // per-layout locations
284  // TODO: per-frame use counting and reset function
285  struct gr_SetCache
286  {
287 
288  // Array doing linear lookup, fine if number of sets small
289  // TODO: could do a hashmap
291  RV_ShaderVariableSet* myLastSet = nullptr;
292 
294  RV_Render *r,
295  int set_num,
297  bool *out_is_new = nullptr);
298  };
301 
302  // NOTE: individual shader blocks may not work if object
303  // drawn multiple times
304  //
305  // In the ideal case: the Specific GR_GeoRender child class
306  // knows/controls the Shaders it will be using and
307  // knows how many Descriptor Sets / memory blocks that requires
308  // (and minus those managed by the RV_Geometry)
309  // ..
310  // Note this ONLY works for the case where the object is drawn once
311  // because:
312  // - uploads can't be done inside a render pass
313  // - so uniforms can't be updated during a render pass
314  // - so all uniforms have to be setup pre-render pass
315  // - so, if some GeoRender needs to be updated/drawn multiple
316  // times in a single pass, will need multiple allocated ubo's
317  // could fix by baking some re-allocation on-upload functionality into
318  // ShaderBlock (like is already done in the Descriptor Set class)
319  //UT_UniquePtr<RV_ShaderVariableSet> myObjectSet;
320  //UT_UniquePtr<RV_ShaderVariableSet> myGlobalSet;
321  //UT_UniquePtr<RV_ShaderVariableSet> myShaderSet;
322 
323 
328 
330 
332 
333 private:
334  UT_UniquePtr<RV_VKBuffer> myNormalScratchBuffer;
335 };
336 
337 
338 // --------------------------------------------------
339 // GR_FillAttributesVK
340 //
341 // Helper class to fill attributes, represented as gr_AttribInfoVK
343 {
344 public:
345 
347  const GT_Primitive *prim,
348  const GU_Detail *geo,
349  const GR_DisplayOption *opts,
350  int num_points);
352 
353  int entries() const { return myAttribs.entries(); }
354 
355  bool hasBounds() const { return myHasPosBounds || myHasDetailBounds; }
356  bool hasCLNormal() const { return myHasCLNormal; }
357  const UT_BoundingBox getBounds() const
358  {
359  if (myHasPosBounds)
360  {
361  return myPosBounds;
362  }
363  if (myHasDetailBounds)
364  {
365  return myDetailBounds;
366  }
367  return UT_BoundingBox();
368  }
369 
370  void allocateStagingBuffer(RV_Render* r);
371 
372  void process(const UT_BlockedRange<int> &range) const;
373 
374  gr_AttribInfoVK *append(
375  const char *name,
376  gr_FillType t,
377  RV_AttributeType at,
378  const GT_DataArrayHandle &h,
380  int size = 0,
384  int instance_group = -1);
385 
386  void upload(RV_Render *r, RV_Geometry *geo);
387 
389  { return myFillAmount; }
390  void addToFillAmount(int n)
391  { myFillAmount += n; }
392 
393  // Attribute default values (if they don't exist).
394  void setFallbacks(const UT_Options *fallbacks);
395  void addIntFallback(const UT_StringHolder &name, int value);
396  void addFallback(const UT_StringHolder &name, fpreal value);
397  void addFallback(const UT_StringHolder &name, const UT_Vector2F &value);
398  void addFallback(const UT_StringHolder &name, const UT_Vector3F &value);
399  void addFallback(const UT_StringHolder &name, const UT_Vector4F &value);
400 
401  bool getFallback(const UT_StringHolder &name, int &value) const;
402  bool getFallback(const UT_StringHolder &name, fpreal &value) const;
403  bool getFallback(const UT_StringHolder &name, UT_Vector2F &value) const;
404  bool getFallback(const UT_StringHolder &name, UT_Vector3F &value) const;
405  bool getFallback(const UT_StringHolder &name, UT_Vector4F &value) const;
406  UT_OptionType getFallbackType(const UT_StringHolder &name) const;
407 
408  bool usedFallback() const { return myUsedFallback; }
409  void setUsedFallback(bool used) { myUsedFallback = used; }
410  void setAttribCache(gr_AttribCacheVK *cache) { myAttribCache = cache; }
411 private:
412  const void *fillConstAttribute(gr_AttribInfoVK *attrib) const;
413  const void *fillRegularAttribute(gr_AttribInfoVK *attrib) const;
414  const void *fillPointAttribute(gr_AttribInfoVK *attrib) const;
415  const void *fillPointIDAttribute(gr_AttribInfoVK *attrib) const;
416  const void *fillPrimIDAttribute(gr_AttribInfoVK *attrib) const;
417  const void *fillStringAttribute(gr_AttribInfoVK *attrib) const;
418  const void *fillMaterialAttribute(gr_AttribInfoVK *attrib) const;
419  const void *fillTangentAttribute(gr_AttribInfoVK *attrib) const;
420  const void *fillBoneAttributes(gr_AttribInfoVK *attrib) const;
421 
422  void createNormalAttribute(gr_AttribInfoVK *attrib) const;
423 
424  RV_Render *myRender;
425  const GU_Detail *myDetail;
426  const GR_DisplayOption *myDisplayOpts;
427  const GT_Primitive *myPrim;
428  gr_AttribCacheVK *myAttribCache;
429  UT_Array<gr_AttribInfoVK *> myAttribs;
430  int myNumPoints;
431 
432  UT_UniquePtr<RV_VKBuffer> myStagingBuffer;
433 
434  void computeDetailBounds();
435  bool myHasDetailBounds;
436  UT_BoundingBox myDetailBounds;
437 
438  bool myHasPosBounds;
439  UT_BoundingBox myPosBounds;
440 
441  UT_Array<GT_DataArrayHandle> myDataHandles;
442  int myFillAmount;
443 
444  UT_Options myFallbacks;
445  bool myUsedFallback = false;
446  bool myHasCLNormal = false;
447 
448  friend gr_AttribInfoVK;
449 };
450 
451 
452 #endif
A collection of Vulkan UBO, SSBO, and Image shader bindings (descriptor set)
UT_UniquePtr< RV_VKImage > myColorOverrideRampBuffer
bool hasCLNormal() const
virtual void gpuMemoryUsage(exint &used_bytes, exint &unused_bytes, exint &pinned_bytes)
Definition: GR_GeoRender.h:197
GLenum GLint * range
Definition: glcorearb.h:1925
void addToFillAmount(int n)
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
UT_OptionType
Definition: UT_Options.h:44
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
gr_SetCache myObjectSet
GLuint buffer
Definition: glcorearb.h:660
GR_API bool createOrReuseSet(RV_Render *r, UT_UniquePtr< RV_ShaderVariableSet > &set, int set_num, const RV_ShaderProgramBase *shader, bool *created=nullptr, const char *name=nullptr)
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
set of parameters sent to GR_Primitive::update()
UT_Array< UT_UniquePtr< RV_ShaderVariableSet > > mySets
const UT_BoundingBox getBounds() const
GLdouble n
Definition: glcorearb.h:2008
GR_BasePrimType
Definition: GR_Defines.h:371
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
UT_IntrusivePtr< GT_DataArray > GT_DataArrayHandle
Definition: GT_DataArray.h:33
RV_AttributeType
Definition: RV_Type.h:99
UT_BoundingBoxT< float > UT_BoundingBox
Definition: GEO_Detail.h:41
UT_UniquePtr< RV_ShaderBlock > myColorOverrideBlock
UT_UniquePtr< gr_AttribCacheVK > myAttribCache
int entries() const
virtual void getVkDecorConnectGroup(RV_PrimType render_prim, bool is_uv_view, int &out_connect_start, int &out_connect_num)
RV_API void cleanupShaders()
#define GR_API
Definition: GR_API.h:10
GR_API GR_SelectMode buildSelection(UT_Array< uint8 > &selected, const GR_UpdateParms &p, GT_DataArrayHandle prim_id, GT_DataArrayHandle pnt_id, GT_DataArrayHandle vert_id, bool is_offset, int repeat_count)
GLuint const GLchar * name
Definition: glcorearb.h:786
The base class for all GT primitive types.
Definition: GT_Primitive.h:43
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:48
GLdouble t
Definition: glad.h:2397
GT_Owner
Definition: GT_Types.h:90
RV_API bool initShaders(RV_Instance *inst)
gr_SetCache myShaderSet
RV_PrimType
Definition: RV_Type.h:356
GLsizeiptr size
Definition: glcorearb.h:664
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
GLuint shader
Definition: glcorearb.h:785
A map of string to various well defined value types.
Definition: UT_Options.h:87
UT_UniquePtr< GR_FillAttributesVK > GR_FillAttributesVKPtr
UT_UniquePtr< RV_ShaderBlock > myTransformBlock
fpreal64 fpreal
Definition: SYS_Types.h:283
bool hasBounds() const
void setUsedFallback(bool used)
bool usedFallback() const
virtual bool supportsRayTrace() const
bool process(T &func, UT_WorkBuffer &fullpath, exint fullpath_len, const UT_StringArray &paths, const UT_Array< FS_Stat > &stats)
Utility function to process the contents of the traverse() function.
Definition: FS_Traverse.h:24
Simple class for a mutli-integer cache tag.
gr_FillType
GLboolean r
Definition: glcorearb.h:1222
set of parameters sent to GR_Primitive::update()
Definition: GR_Uniforms.h:56
void setAttribCache(gr_AttribCacheVK *cache)
A vulkan buffer object.
Definition: RV_VKBuffer.h:75
UT_UniquePtr< RV_Geometry > myGeometryVk
Declare prior to use.
RV_Geometry * getVkGeometry() override
GR_SelectMode
Definition: GR_Defines.h:246