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