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 
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  /// For primitives that cannot be culled, this at least notifies the
55  /// manager of the GL primitive count so it can be taken into account.
56  void queueUncullableDraw(int num_gl_prims);
57 
58  /// Request a new draw ID used to queue requests. Return it when done.
59  int newDrawRequestID();
60  void returnDrawRequestID(int draw_id);
61 
62  // Submit a draw request of a single object with 'nprims' GL primitives
63  // within bounding box 'box' and having a full world transform of
64  // 'world_mat'. Returns an ID which is used to fetch the results.
65  void queueDrawRequest(const RE_Render *r,
66  int draw_id,
67  int num_gl_prims,
68  const UT_BoundingBox &box,
69  const UT_Matrix4D &world_mat,
70  GR_RenderMode rmode,
71  GR_RenderFlags rflags,
72  const GR_DrawParms &dp,
73  bool wireframe,
74  int32 prim_id,
75  bool selected,
76  UT_Vector3F *color = nullptr);
77 
78  // queue a list of instances to draw with different transforms.
79  // If only some of the draws should be performed, 'result_visible_indices'
80  // will indicte which ones.
81  void queueInstanceDrawRequest(const RE_Render *r,
82  int draw_id,
83  int num_gl_prims,
84  const UT_BoundingBox &box,
85  const UT_Matrix4D &obj_transform,
86  const UT_Matrix4DArray &instances,
87  UT_IntArray &result_visible_indices,
88  UT_Vector2i &result_visible_range,
89  GR_RenderMode rmode,
90  GR_RenderFlags rflags,
91  const GR_DrawParms &dp,
92  bool wireframe,
93  const GT_DataArrayHandle &prim_ids,
94  const UT_Array<uint8> *selected,
95  const UT_Vector3FArray *colors,
96  bool all_selected = false);
97  void queueNestedInstanceDrawRequest(const RE_Render *r,
98  int draw_id,
99  int num_gl_prims,
100  const UT_BoundingBox &box,
101  const UT_Matrix4D &obj_transform,
102  const UT_Matrix4DArray &instances,
103  const UT_IntArray &nest_level_counts,
104  UT_IntArray &result_visible_indices,
105  UT_Vector2i &result_visible_range,
106  GR_RenderMode rmode,
107  GR_RenderFlags rflags,
108  const GR_DrawParms &dp,
109  bool wireframe,
110  const GT_DataArrayHandle &prim_ids,
111  const UT_Array<uint8> *selected,
112  const UT_Vector3FArray *colors,
113  bool all_selected = false);
114 
115  // queue a partial list of instances to draw from a full list.
116  // 'visible_indices' must refer to entries in the 'instances' array.
117  // If only some of the draws should be performed, 'result_visible_indices'
118  // will indicte which ones. draw_index will be assigned the index to query
119  // later on getDrawRequestResult().
120  void queuePartialInstanceDrawRequest(const RE_Render *r,
121  int draw_id,
122  int num_gl_prims,
123  const UT_BoundingBox &box,
124  const UT_Matrix4D &obj_transform,
125  const UT_Matrix4DArray &instances,
126  const UT_IntArray &visible_indices,
127  UT_IntArray &result_visible_indices,
128  UT_Vector2i &result_visible_range,
129  GR_RenderMode rmode,
130  GR_RenderFlags rflags,
131  const GR_DrawParms &dp,
132  bool wireframe,
133  const GT_DataArrayHandle &prim_ids,
134  const UT_Array<uint8> *selected,
135  const UT_Vector3FArray *colors,
136  bool all_selected = false);
137 
138  // queue a partial list of nested instances to draw from a full list.
139  // This is the same as the above method, but `nest_level_counts` determines
140  // the number of instances in each level (index 0 is the #leaf instance).
141  void queuePartialNestedInstanceRequest(const RE_Render *r,
142  int draw_id,
143  int num_gl_prims,
144  const UT_BoundingBox &box,
145  const UT_Matrix4D &obj_transform,
146  const UT_Matrix4DArray &instances,
147  const UT_IntArray &visible_indices,
148  const UT_IntArray &nest_level_counts,
149  UT_IntArray &result_visible_indices,
150  UT_Vector2i &result_visible_range,
151  GR_RenderMode rmode,
152  GR_RenderFlags rflags,
153  const GR_DrawParms &dp,
154  bool wireframe,
155  const GT_DataArrayHandle &prim_ids,
156  const UT_Array<uint8> *selected,
157  const UT_Vector3FArray *colors,
158  bool all_selected = false);
159 
160  /// Request a new draw ID used to queue requests. Return it when done.
161  int newBBoxRequestID();
162  void returnBBoxRequestID(int bbox_id);
163 
164  // Queue a single bounding box to be drawn.
165  void queueBBoxDraw(const RE_Render *r,
166  int bbox_id,
167  const UT_BoundingBox &box,
168  const UT_Matrix4D &obj_transform,
169  bool wireframe,
170  int32 prim_id,
171  bool selected,
172  UT_Vector3F *color = nullptr);
173 
174  // Queue a list of bounding boxes to be drawn (one bounding box transormed)
175  void queueBBoxDraw(const RE_Render *r,
176  int bbox_id,
177  const UT_BoundingBox &box,
178  const UT_Matrix4D &obj_transform,
179  const UT_Matrix4DArray &instances,
180  bool wireframe,
181  const GT_DataArrayHandle &prim_ids, //size of 'instances'
182  const UT_Array<uint8> *selected, //size of 'instances'
183  const UT_Vector3FArray *colors, //size of 'instances'
184  bool all_selected = false); // everything selected
185 
186  // Queue a partial list of bounding boxes to be drawn (one bbox transformed
187  // by some of the transforms, indexed by visible_instances).
188  void queuePartialBBoxDraw(
189  const RE_Render *r,
190  int bbox_id,
191  const UT_BoundingBox &box,
192  const UT_Matrix4D &obj_transform,
193  const UT_Matrix4DArray &instances,
194  const UT_IntArray &visible_indices,
195  bool wireframe,
196  const GT_DataArrayHandle &prim_ids, //size of 'instances'
197  const UT_Array<uint8> *selected, //size of 'instances'
198  const UT_Vector3FArray *colors, // size of ^^
199  bool all_selected = false); // everything selected
200 
202  {
203  NO_DRAW, // no instances to draw
204  PARTIAL_DRAW, // some instances, flagged ones drawn
205  POINTS_ONLY_DRAW, // draw all as points only
206  FULL_DRAW, // draw all instances as full geometry
207 
208  ERROR_NO_SUCH_ID, // the draw_id passed is invalid.
209  ERROR_NOT_PROCESSED // called too early, before all requests were
210  }; // processed.
211 
212  // Get the result of the global processing. 'visible instances' from the
213  // queued draw requests will only be set if the result is PARTIAL_DRAW.
214  // If it is somehow visible, the render mode, flags and draw parms will be
215  // set.
216  gr_DrawRequestResult getDrawRequestResult(int draw_id,
219  GR_DrawParms &parms) const;
220 
221 private:
222  void beginPass(const GR_CommonDispOption &opts);
223  void processDrawRequests(RE_Render *r,
224  const GR_CommonDispOption &opts);
225  void processQueuedBBoxes(RE_Render *r,
226  const GR_CommonDispOption &opts);
227  void endPass(RE_Render *r, const GR_CommonDispOption &opts);
228  void updateBBoxes(RE_Render *r);
229  void drawBBoxes(RE_Render *r, const GR_CommonDispOption &opts);
230  void generatePickID(const RE_Render *r, UT_Vector4i &pick_id);
231  bool determineBBoxMode(bool wire_req) const;
232 
233 
234  static void initialize();
235 
237  virtual ~GR_InstanceManager();
238 
239  class gr_Instance
240  {
241  public:
242  GT_PrimitiveHandle myInstance;
243  UT_BoundingBoxF myBBox;
244  int64 myLRU;
245  int64 myVersion;
246  int64 mySize;
247  int myUsage;
248  };
249 
251  int64 myLRU;
252 
253 public:
255  {
256  public:
258  const UT_Matrix4D *si,
259  const UT_Matrix4DArray *insts,
260  const UT_BoundingBox &bb,
261  const UT_IntArray *vi,
262  UT_IntArray *vr,
263  GR_RenderMode rm,
264  GR_RenderFlags rf,
265  GR_DrawParms dp,
266  int passid)
267  : num_prims(np), single_inst(si), instances(insts), bbox(&bb),
268  visible_instances(vi), visible_result(vr), selected(false),
269  result(ERROR_NOT_PROCESSED), rmode(rm), rflags(rf), dparms(dp),
270  pass_id(passid), colors(nullptr), color(nullptr),
271  nest_counts(nullptr), selected_prims(nullptr), wireframe(false),
272  visible_range(nullptr) {}
274  : dparms(nullptr, nullptr, -1, false, 0, 0,0,0),
275  pass_id(-1), num_prims(0), single_inst(nullptr),
276  instances(nullptr), bbox(nullptr), nest_counts(nullptr),
277  visible_instances(nullptr), visible_result(nullptr),
278  selected_prims(nullptr), color(nullptr), visible_range(nullptr) {}
279 
280  int pass_id;
298  bool selected;
299  bool wireframe;
300  };
301 
303  {
304  public:
306  const UT_Matrix4DArray *insts,
307  const UT_IntArray *nesting,
308  const UT_BoundingBox &bb,
309  const UT_IntArray *vi,
310  int passid)
311  : single_inst(si),instances(insts),bbox(&bb),visible_instances(vi),
312  offset(0), bucket(0), pass_id(passid), selected_prims(nullptr),
313  colors(nullptr), selected(false), wireframe(false),
314  color(nullptr), nest_counts(nesting)
315  {}
317  : single_inst(nullptr),instances(nullptr),bbox(nullptr),
318  visible_instances(nullptr), offset(0), bucket(0), pass_id(-1),
319  selected_prims(nullptr),colors(nullptr), selected(false),
320  wireframe(false), color(nullptr), nest_counts(nullptr)
321  {}
332  bool selected;
333  bool wireframe;
334 
335  // the list to write to.
336  int offset;
337  int bucket;
338  int pass_id;
339  };
341  {
342  public:
343  gr_DrawEntry() : z(0.0), draw_id(0), draw_index(0) {}
344  gr_DrawEntry(fpreal32 _z, int did, int didx)
345  : z(_z), draw_id(did), draw_index(didx) {}
347  int draw_id;
349  };
350 
351 
352  enum
353  {
354  BBOX_COLOR_SHADED=0,
355  BBOX_COLOR_WIRE=1,
356  BBOX_SHADED =2,
357  BBOX_WIRE =3,
358 
359  NUM_BBOX_SHADING_MODES = 4,
360  NUM_BBOX_COLOR_MODES = 2
361  };
362 
363 private:
364  UT_Array<gr_DrawRequest> myDrawRequests;
365  UT_Array<gr_BBoxRequest> myBBoxRequests;
366  UT_Array<UT_Array<gr_DrawEntry> > myBucketIndex;
367  UT_Matrix4FArray myBBoxTransforms;
368  UT_Array<int8> myBBoxCategories;
369  UT_Array<const UT_Vector3F *> myBBoxColors;
370  SYS_AtomicInt64 myGLPrimCount;
371  SYS_AtomicInt64 myNumBBoxRequests;
372  fpreal myLastZ;
373  fpreal myLastMinZ;
374  bool myReducedDraws;
375  bool myIsActive;
376  bool myInteractiveMode;
377  bool myAllowBBoxes;
378  UT_Vector3F mySelectionColor;
380  int myPassID;
381  UT_IntArray myFreeDrawIDs;
382  UT_IntArray myFreeBBoxIDs;
383  UT_Vector3FArray mySplitBBoxColors[NUM_BBOX_COLOR_MODES];
384  UT_Matrix4FArray mySplitBBoxTransforms[NUM_BBOX_SHADING_MODES];
385 
386  static GR_InstanceManager *theInstance;
387 
388  // These two are friends for the pass management and initialization.
389  friend class GUI_SceneFeel;
390  friend class DM_VPortAgent;
391 };
392 
393 #endif
const UT_Array< uint8 > * selected_prims
gr_DrawEntry(fpreal32 _z, int did, int didx)
bool isActive() const
GLenum mode
Definition: glew.h:2163
const UT_Array< uint8 > * selected_prims
gr_DrawEntry()
GLenum GLsizei GLsizei GLsizei GLsizei GLbitfield flags
Definition: glew.h:2864
const UT_Matrix4DArray * instances
GLdouble GLdouble z
Definition: glew.h:1559
long long int64
Definition: SYS_Types.h:111
GR_RenderMode
Definition: GR_Defines.h:44
const UT_Matrix4DArray * instances
#define GR_API
Definition: GR_API.h:10
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:318
GLuint color
Definition: glew.h:7902
int int32
Definition: SYS_Types.h:39
GR_RenderFlags
Definition: GR_Defines.h:80
void allowBBoxReplacement(bool allow)
double fpreal
Definition: SYS_Types.h:276
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
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
fpreal32 z
GLuint64EXT * result
Definition: glew.h:14007
bool canQueueRequests() const
int draw_index
float fpreal32
Definition: SYS_Types.h:195
gr_BBoxRequest(const UT_Matrix4D *si, const UT_Matrix4DArray *insts, const UT_IntArray *nesting, const UT_BoundingBox &bb, const UT_IntArray *vi, int passid)
GLintptr offset
Definition: glew.h:1682