HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GR_InstanceManager.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_InstanceManager (C++)
7  *
8  * COMMENTS:
9  * Global instance cache and global draw pass management
10  *
11  */
12 #ifndef GR_InstanceManager_h
13 #define GR_InstanceManager_h
14 
15 
16 #include "GR_API.h"
17 
18 #include <GT/GT_Primitive.h>
19 #include <UT/UT_BoundingBox.h>
20 #include <UT/UT_Map.h>
21 #include <UT/UT_StringMap.h>
22 #include <UT/UT_NonCopyable.h>
23 #include <UT/UT_StringHolder.h>
24 #include <UT/UT_VectorTypes.h>
25 #include <SYS/SYS_AtomicInt.h>
26 
27 #include "GR_Defines.h"
28 #include "GR_CommonDispOption.h"
29 #include "GR_DrawParms.h"
30 
31 class RV_Instance;
32 
34 {
35 public:
36  static GR_InstanceManager &get() { return *theInstance; }
37 
38  // ------------------------------------------------------------------
39  // Deferred Instance logging
40  //
41  // This allows you to register what you will be drawing, have everything
42  // be globally processed to ensure a max poly count is respected, and get
43  // the results of that
44  // The bouding box, matrices, and arrays must not be deleted until the
45  // request has been processed and you call getDrawRequestResult().
46 
47  bool isActive() const { return myIsActive; }
48 
49  bool canQueueRequests() const
50  { return myIsActive && myInteractiveMode; }
51 
52  void allowBBoxReplacement(bool allow) { myAllowBBoxes = allow; }
53 
54  void setDrawSolidBBoxes(bool draw)
55  { UT_ASSERT(!myIsActive); if (!myIsActive) { myDrawSolidBBoxes = draw;} }
56  void setDrawWireBBoxes(bool draw)
57  { UT_ASSERT(!myIsActive); if (!myIsActive) { myDrawWireBBoxes = draw;} }
58 
59 
60  /// For primitives that cannot be culled, this at least notifies the
61  /// manager of the GL primitive count so it can be taken into account.
62  void queueUncullableDraw(int num_gl_prims);
63 
64  /// Request a new draw ID used to queue requests. Return it when done.
65  int newDrawRequestID();
66  void returnDrawRequestID(int draw_id);
67 
68  // Submit a draw request of a single object with 'nprims' GL primitives
69  // within bounding box 'box' and having a full world transform of
70  // 'world_mat'. Returns an ID which is used to fetch the results.
71  void queueDrawRequest(const RE_RenderContext r,
72  int draw_id,
73  int num_gl_prims,
74  const UT_BoundingBox &box,
75  const UT_Matrix4D &world_mat,
76  GR_RenderMode rmode,
77  GR_RenderFlags rflags,
78  const GR_DrawParms &dp,
79  bool wireframe,
80  int32 prim_id,
81  bool selected,
82  UT_Vector3F *color = nullptr);
83 
84  // queue a list of instances to draw with different transforms.
85  // If only some of the draws should be performed, 'result_visible_indices'
86  // will indicte which ones.
87  void queueInstanceDrawRequest(const RE_RenderContext r,
88  int draw_id,
89  int num_gl_prims,
90  const UT_BoundingBox &box,
91  const UT_Matrix4D &obj_transform,
92  const UT_Matrix4DArray &instances,
93  UT_IntArray &result_visible_indices,
94  UT_Vector2i &result_visible_range,
95  GR_RenderMode rmode,
96  GR_RenderFlags rflags,
97  const GR_DrawParms &dp,
98  bool wireframe,
99  const GT_DataArrayHandle &prim_ids,
100  const UT_Array<uint8> *selected,
101  const UT_Vector3FArray *colors,
102  bool all_selected = false);
103  void queueNestedInstanceDrawRequest(const RE_RenderContext r,
104  int draw_id,
105  int num_gl_prims,
106  const UT_BoundingBox &box,
107  const UT_Matrix4D &obj_transform,
108  const UT_Matrix4DArray &instances,
109  const UT_IntArray &nest_level_counts,
110  UT_IntArray &result_visible_indices,
111  UT_Vector2i &result_visible_range,
112  GR_RenderMode rmode,
113  GR_RenderFlags rflags,
114  const GR_DrawParms &dp,
115  bool wireframe,
116  const GT_DataArrayHandle &prim_ids,
117  const UT_Array<uint8> *selected,
118  const UT_Vector3FArray *colors,
119  bool all_selected = false);
120 
121  // queue a partial list of instances to draw from a full list.
122  // 'visible_indices' must refer to entries in the 'instances' array.
123  // If only some of the draws should be performed, 'result_visible_indices'
124  // will indicte which ones. draw_index will be assigned the index to query
125  // later on getDrawRequestResult().
126  void queuePartialInstanceDrawRequest(const RE_RenderContext r,
127  int draw_id,
128  int num_gl_prims,
129  const UT_BoundingBox &box,
130  const UT_Matrix4D &obj_transform,
131  const UT_Matrix4DArray &instances,
132  const UT_IntArray &visible_indices,
133  UT_IntArray &result_visible_indices,
134  UT_Vector2i &result_visible_range,
135  GR_RenderMode rmode,
136  GR_RenderFlags rflags,
137  const GR_DrawParms &dp,
138  bool wireframe,
139  const GT_DataArrayHandle &prim_ids,
140  const UT_Array<uint8> *selected,
141  const UT_Vector3FArray *colors,
142  bool all_selected = false);
143 
144  // queue a partial list of nested instances to draw from a full list.
145  // This is the same as the above method, but `nest_level_counts` determines
146  // the number of instances in each level (index 0 is the #leaf instance).
147  void queuePartialNestedInstanceRequest(const RE_RenderContext r,
148  int draw_id,
149  int num_gl_prims,
150  const UT_BoundingBox &box,
151  const UT_Matrix4D &obj_transform,
152  const UT_Matrix4DArray &instances,
153  const UT_IntArray &visible_indices,
154  const UT_IntArray &nest_level_counts,
155  UT_IntArray &result_visible_indices,
156  UT_Vector2i &result_visible_range,
157  GR_RenderMode rmode,
158  GR_RenderFlags rflags,
159  const GR_DrawParms &dp,
160  bool wireframe,
161  const GT_DataArrayHandle &prim_ids,
162  const UT_Array<uint8> *selected,
163  const UT_Vector3FArray *colors,
164  bool all_selected = false);
165 
166  /// Request a new draw ID used to queue requests. Return it when done.
167  int newBBoxRequestID();
168  void returnBBoxRequestID(int bbox_id);
169 
170  // Queue a single bounding box to be drawn.
171  void queueBBoxDraw(const RE_RenderContext r,
172  int bbox_id,
173  const UT_BoundingBox &box,
174  const UT_Matrix4D &obj_transform,
175  bool wireframe,
176  int32 prim_id,
177  bool selected,
178  UT_Vector3F *color = nullptr);
179 
180  // Queue a list of bounding boxes to be drawn (one bounding box transormed)
181  void queueBBoxDraw(const RE_RenderContext r,
182  int bbox_id,
183  const UT_BoundingBox &box,
184  const UT_Matrix4D &obj_transform,
185  const UT_Matrix4DArray &instances,
186  bool wireframe,
187  const GT_DataArrayHandle &prim_ids, //size of 'instances'
188  const UT_Array<uint8> *selected, //size of 'instances'
189  const UT_Vector3FArray *colors, //size of 'instances'
190  bool all_selected = false); // everything selected
191 
192  // Queue a partial list of bounding boxes to be drawn (one bbox transformed
193  // by some of the transforms, indexed by visible_instances).
194  void queuePartialBBoxDraw(
195  const RE_RenderContext r,
196  int bbox_id,
197  const UT_BoundingBox &box,
198  const UT_Matrix4D &obj_transform,
199  const UT_Matrix4DArray &instances,
200  const UT_IntArray &visible_indices,
201  bool wireframe,
202  const GT_DataArrayHandle &prim_ids, //size of 'instances'
203  const UT_Array<uint8> *selected, //size of 'instances'
204  const UT_Vector3FArray *colors, // size of ^^
205  bool all_selected = false); // everything selected
206 
207  static bool initShaders(RV_Instance* inst);
208  static void cleanupShaders();
209 
211  {
212  NO_DRAW, // no instances to draw
213  PARTIAL_DRAW, // some instances, flagged ones drawn
214  POINTS_ONLY_DRAW, // draw all as points only
215  FULL_DRAW, // draw all instances as full geometry
216 
217  ERROR_NO_SUCH_ID, // the draw_id passed is invalid.
218  ERROR_NOT_PROCESSED // called too early, before all requests were
219  }; // processed.
220 
221  // Get the result of the global processing. 'visible instances' from the
222  // queued draw requests will only be set if the result is PARTIAL_DRAW.
223  // If it is somehow visible, the render mode, flags and draw parms will be
224  // set.
225  gr_DrawRequestResult getDrawRequestResult(int draw_id,
228  GR_DrawParms &parms) const;
229 
230 private:
231  void beginPass(const GR_CommonDispOption &opts);
232  void processDrawRequests(RE_RenderContext rc,
233  const GR_CommonDispOption &opts);
234  void processQueuedBBoxes(RE_RenderContext rc,
235  const GR_CommonDispOption &opts);
236  void endPass(RE_RenderContext rc, const GR_CommonDispOption &opts);
237  void updateBBoxes(RE_RenderContext rc);
238  void drawBBoxes(RE_RenderContext rc, const GR_CommonDispOption &opts);
239  void generatePickID(const RE_RenderContext r, UT_Vector4i &pick_id);
240  bool determineBBoxMode(bool wire_req) const;
241 
242 
243  static void initialize();
244 
246  virtual ~GR_InstanceManager();
247 
248  class gr_Instance
249  {
250  public:
251  GT_PrimitiveHandle myInstance;
252  UT_BoundingBoxF myBBox;
253  int64 myLRU;
254  int64 myVersion;
255  int64 mySize;
256  int myUsage;
257  };
258 
260  int64 myLRU;
261 
262 public:
264  {
265  public:
267  const UT_Matrix4D *si,
268  const UT_Matrix4DArray *insts,
269  const UT_BoundingBox &bb,
270  const UT_IntArray *vi,
271  UT_IntArray *vr,
272  GR_RenderMode rm,
273  GR_RenderFlags rf,
274  GR_DrawParms dp,
275  int passid)
276  : num_prims(np), single_inst(si), instances(insts), bbox(&bb),
277  visible_instances(vi), visible_result(vr), selected(false),
278  result(ERROR_NOT_PROCESSED), rmode(rm), rflags(rf), dparms(dp),
279  pass_id(passid), colors(nullptr), color(nullptr),
280  nest_counts(nullptr), selected_prims(nullptr), wireframe(false),
281  visible_range(nullptr) {}
283  : dparms(nullptr, nullptr, 0, false, 0, 0,0,0),
284  pass_id(-1), num_prims(0), single_inst(nullptr),
285  instances(nullptr), bbox(nullptr), nest_counts(nullptr),
286  visible_instances(nullptr), visible_result(nullptr),
287  selected_prims(nullptr), color(nullptr), visible_range(nullptr) {}
288 
289  int pass_id;
307  bool selected;
308  bool wireframe;
309  };
310 
312  {
313  public:
315  const UT_Matrix4DArray *insts,
316  const UT_IntArray *nesting,
317  const UT_BoundingBox &bb,
318  const UT_IntArray *vi,
319  int passid)
320  : single_inst(si),instances(insts),bbox(&bb),visible_instances(vi),
321  offset(0), bucket(0), pass_id(passid), selected_prims(nullptr),
322  colors(nullptr), selected(false), wireframe(false),
323  color(nullptr), nest_counts(nesting)
324  {}
326  : single_inst(nullptr),instances(nullptr),bbox(nullptr),
327  visible_instances(nullptr), offset(0), bucket(0), pass_id(-1),
328  selected_prims(nullptr),colors(nullptr), selected(false),
329  wireframe(false), color(nullptr), nest_counts(nullptr)
330  {}
341  bool selected;
342  bool wireframe;
343 
344  // the list to write to.
345  int offset;
346  int bucket;
347  int pass_id;
348  };
350  {
351  public:
352  gr_DrawEntry() : z(0.0), draw_id(0), draw_index(0) {}
353  gr_DrawEntry(fpreal32 _z, int did, int didx)
354  : z(_z), draw_id(did), draw_index(didx) {}
356  int draw_id;
358  };
359 
360 
361  enum
362  {
363  BBOX_COLOR_SHADED=0,
364  BBOX_COLOR_WIRE=1,
365  BBOX_SHADED =2,
366  BBOX_WIRE =3,
367 
368  NUM_BBOX_SHADING_MODES = 4,
369  NUM_BBOX_COLOR_MODES = 2
370  };
371 
372 private:
373  UT_Array<gr_DrawRequest> myDrawRequests;
374  UT_Array<gr_BBoxRequest> myBBoxRequests;
375  UT_Array<UT_Array<gr_DrawEntry> > myBucketIndex;
376  UT_Matrix4FArray myBBoxTransforms;
377  UT_Array<int8> myBBoxCategories;
378  UT_Array<const UT_Vector3F *> myBBoxColors;
379  SYS_AtomicInt64 myGLPrimCount;
380  SYS_AtomicInt64 myNumBBoxRequests;
381  fpreal myLastZ;
382  fpreal myLastMinZ;
383  bool myReducedDraws;
384  bool myIsActive;
385  bool myInteractiveMode;
386  bool myAllowBBoxes;
387  bool myDrawSolidBBoxes;
388  bool myDrawWireBBoxes;
389  UT_Vector3F mySelectionColor;
391  int myPassID;
392  UT_IntArray myFreeDrawIDs;
393  UT_IntArray myFreeBBoxIDs;
394  UT_Vector4FArray mySplitBBoxColors[NUM_BBOX_COLOR_MODES];
395  UT_Matrix4FArray mySplitBBoxTransforms[NUM_BBOX_SHADING_MODES];
396 
397  static GR_InstanceManager *theInstance;
398 
399  // These two are friends for the pass management and initialization.
400  friend class GUI_SceneFeel;
401  friend class DM_VPortAgent;
402 };
403 
404 #endif
const UT_Array< uint8 > * selected_prims
GLbitfield flags
Definition: glcorearb.h:1596
int int32
Definition: SYS_Types.h:39
gr_DrawEntry(fpreal32 _z, int did, int didx)
bool isActive() const
const UT_Array< uint8 > * selected_prims
gr_DrawEntry()
void setDrawSolidBBoxes(bool draw)
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
const UT_Matrix4DArray * instances
**But if you need a result
Definition: thread.h:622
float fpreal32
Definition: SYS_Types.h:200
Temporary container for either a RV_Render and an RE_Render.
GLintptr offset
Definition: glcorearb.h:665
GR_RenderMode
Definition: GR_Defines.h:48
const UT_Matrix4DArray * instances
void setDrawWireBBoxes(bool draw)
RV_API void cleanupShaders()
#define GR_API
Definition: GR_API.h:10
long long int64
Definition: SYS_Types.h:116
OPENVDB_API void initialize()
Global registration of native Grid, Transform, Metadata and Point attribute types. Also initializes blosc (if enabled).
Definition: logging.h:294
GR_RenderFlags
Definition: GR_Defines.h:88
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:48
GLenum mode
Definition: glcorearb.h:99
RV_API bool initShaders(RV_Instance *inst)
void allowBBoxReplacement(bool allow)
GA_API const UT_StringHolder parms
GLuint color
Definition: glcorearb.h:1261
fpreal64 fpreal
Definition: SYS_Types.h:283
gr_DrawRequest(int np, const UT_Matrix4D *si, const UT_Matrix4DArray *insts, const UT_BoundingBox &bb, const UT_IntArray *vi, UT_IntArray *vr, GR_RenderMode rm, GR_RenderFlags rf, GR_DrawParms dp, int passid)
int draw_id
fpreal32 z
bool canQueueRequests() const
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
GLboolean r
Definition: glcorearb.h:1222
int draw_index
gr_BBoxRequest(const UT_Matrix4D *si, const UT_Matrix4DArray *insts, const UT_IntArray *nesting, const UT_BoundingBox &bb, const UT_IntArray *vi, int passid)