HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_GPUTrace.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: GT_GPUTrace.h (GU Library, C++)
7  *
8  * COMMENTS:
9  * This is located in GT so that BRAY (and others) can pass
10  * GT_PolygonMeshes if they don't have GU_Detail's handy. BRAY might need
11  * to displace/subdivide the geometry.
12  */
13 
14 #ifndef __GT_GPUTrace__
15 #define __GT_GPUTrace__
16 
17 #include "GT_API.h"
18 
19 #include <UT/UT_StringHolder.h>
20 #include <UT/UT_UniquePtr.h>
21 #include <UT/UT_StringSet.h>
22 #include <UT/UT_BitArray.h>
23 #include <GU/GU_DetailHandle.h>
24 #include "GT_PrimPolygonMesh.h"
25 
27 
29 {
30 public:
32 
33  struct GT_API Factory
34  {
35  Factory();
36  virtual ~Factory();
37  UT_NON_COPYABLE(Factory)
38  virtual GT_GPUTracePtr newTrace() const = 0;
39 
40  // Simple name for the tracer (i.e. optix or embree)
41  virtual const UT_StringHolder &name() const = 0;
42  // More descriptive name (i.e. "Optix 8.0.1 driver ..."
43  virtual const UT_StringHolder &label() const = 0;
44  // Priority for the tracer. The faster the execution, the higher the
45  // priority. Embree should probably be 0.
46  virtual int priority() const = 0;
47  };
48 
49  // Register a new factory
50  static void registerFactory(UT_UniquePtr<Factory> factory);
51  // Remove a factory
52  static void removeFactory(const UT_StringRef &name);
53  // List of all factories
54  static void getFactories(UT_Array<const Factory *> &factories);
55 
57  {
59  PHT_GU
60  };
61 
63  {
64  PrimitiveInfo() = default;
66  : myGTHandle(handle)
67  , myHandleType(PHT_GT)
68  { }
70  : myGUHandle(handle)
71  , myHandleType(PHT_GU)
72  { }
73 
75  GT_PrimitiveHandle myGTHandle = nullptr;
76  PrimHandleType myHandleType = PHT_GT;
77 
79 
81 
82  // 0=no culling, 1=back, 2=front
83  int myCullBackFaceMode = 0;
84  int myVisibilityCullBackFaceMode = 0;
85 
87  // visiblity tracesets(comma or space separated list)
90  };
91 
93  {
96  GTDM_Both
97  };
98 
99  // occlusion:
100  // nhits = 0
101  // gather(N, 90, nsamples)
102  // nhits += 1
103  // Oc = 1 - nhits / nsamples
104  // thickness:
105  // nhits = 0;
106  // gather(-N, 90, nsamples)
107  // dist += ray.tfar
108  // Th = dist / nsamples
109  // cavity:
110  // fCv = 0;
111  // cV = 0;
112  // gather(-N, 90, nsamples)
113  // fCv++;
114  // fCv = lerp(1 - (fCv/nsamples), 1, options.myCavityBias);
115  // curvature:
116  // cu_convex = 0
117  // cu_cocave = 0
118  // cu_offset = N * options.myCurvatureSBias
119  // gather(P - cu_offset, -N, 90, nsamples, cosine=true)
120  // cu_convex += 1 - ray_dist / options.myCurvatureSBias
121  // gather(P + cu_offset, N, 90, nsamples, cosine=true)
122  // cu_concave += 1 - ray_dist / options.myCurvatureSBias;
123  // cu_convex /= nsamples
124  // cu_concave /= nsamples
125  // auto vop_bias = [](float base, float bias) {
126  // if (base <= 0) return 0;
127  // if (base >= 1) return 1;
128  // return bias / (((1/base) - 2) * (1-bias + 1))
129  // };
130  // fCu = vop_bias(cu_convex, myOptions.myCurvatureBias)
131  // - vop_bias(cu_concave, myOptions.myCurvatureBias);
132  // Cu = fCu * .5 + .5;
134  {
136  {
137  int totalrays = 0;
138  if (myOcclusion && myOcclusionSamples)
139  totalrays += myOcclusionSamples;
140  if (myCavity && myCavitySamples)
141  totalrays += myCavitySamples;
142  if (myCurvature && myCurvatureSamples)
143  totalrays += 2 * myCurvatureSamples;
144  if (myThickness && myThicknessSamples)
145  totalrays += myThicknessSamples;
146  if (myEdge && myEdgeSamples)
147  totalrays += myEdgeSamples;
148  if (myRoundedNormal && myRoundedNormalSamples)
149  totalrays += myRoundedNormalSamples;
150  return totalrays;
151  }
152  bool needSelfTraceset() const
153  {
154  return (myEdge && myEdgeSamples > 0) ||
155  (myRoundedNormal && myRoundedNormalSamples > 0);
156  }
157 
158  bool myOcclusion = true;
159  int myOcclusionSamples = 64;
160  float myOcclusionMaxDist = -1;
161  float myOcclusionRayBias = 0.001;
162 
163  bool myThickness = true;
164  int myThicknessSamples = 16;
165  float myThicknessRayBias = 0.001;
166 
167  bool myCavity = true;
168  int myCavitySamples = 16;
169  float myCavityMaxDist = -1;
170  float myCavityBias = 0.5;
171  float myCavityRayBias = 0.001;
172 
173  bool myCurvature = true;
174  int myCurvatureSamples = 16;
175  float myCurvatureBias = 0.5;
176  float myCurvatureSDist = 0.1; // ie. ray max distance
177  float myCurvatureScale = 1;
178  float myCurvatureRayBias = 0.002; // myCurvatureSDist * 0.02
179 
180  bool myEdge = true;
181  int myEdgeSamples = 4;
182  float myEdgeRayBias = 0.001;
183  float myEdgeRadius = 0.01f;
184  float myEdgeCuspAngle = 30.f;
185  // 0 concave and convex
186  // 1 concave
187  // 2 convex
188  int myEdgeMode = 0;
189 
190  bool myRoundedNormal = true;
191  int myRoundedNormalSamples = 4;
192  float myRoundedNormalRayBias = 0.001;
193  float myRoundedNormalRadius = 0.01f;
194  float myRoundedNormalCuspAngle = 30.f;
195  int myRoundedNormalMode = 0;
196 
197  float myTraceRayBias = 1e-3f;
198  float myTraceRayTfar = SYS_FP32_MAX;
199 
200  GT_TraceDirectionMode myTraceDirection = GTDM_Both;
201  int myPixelSamples = 1;
202  };
203 
204  /// Allocate a tracer from a particular factory
205  static GT_GPUTracePtr allocTrace(const UT_StringRef &name);
206 
207  /// @{
208  /// Allocate the best tracer for a given piece of geometry
209  static GT_GPUTracePtr allocTrace(
210  const UT_Array<PrimitiveInfo>& prims,
211  const UT_StringRef& name,
212  const BakingOptions& opts);
213  /// @}
214 
215  GT_GPUTrace(const Factory &factory)
216  : myFactory(factory)
217  {
218  };
219  virtual ~GT_GPUTrace() {};
220 
222 
223  const UT_StringHolder &name() const { return myFactory.name(); }
224  const UT_StringHolder &label() const { return myFactory.label(); }
225 
226  enum DataType
227  {
228  DT_Int = 0,
233 
234  DT_NumDataType
235  };
236 
238  {
239  // Attribute name.
241  // Output buffer to write.
243  // Type of the buffer
245  };
246 
248  {
249  BCT_Ao, // Ambient Occlusion
250  BCT_Cv, // Cavity
251  BCT_Cu_N, // Curvature (convex)
252  BCT_Cu_P, // Curvature (concave)
253  BCT_Th, // Thickness
254  BCT_Eg, // Edge
255  BCT_Rn, // Rounded Edge Normal
256 
257  BakingCalcType_Num
258  };
259 
260  // Seeds for various different components
261  static constexpr uint AO_SEED = 0x4c41e0cd;
262  static constexpr uint CV_SEED = 0xf2ee2275;
263  static constexpr uint CU_N_SEED = 0x4b65c61e;
264  static constexpr uint CU_P_SEED = 0x9a4bf788;
265  static constexpr uint TH_SEED = 0x4c910506;
266  static constexpr uint EG_SEED = 0x139fa345;
267  static constexpr uint RN_SEED = 0xe4df7f01;
268  static constexpr uint PIXEL_SEED = 0x73aecd3d;
269 
270  /// @{
271  /// When init fails
272  ///
273  bool init(const UT_Array<PrimitiveInfo>& prims, const BakingOptions& opts);
274  /// @}
275 
276  /// Note the @c dir vectors must be normalized.
277  /// If @c trace_high_res is true, High res mesh info are traced using
278  /// rays with given @c org and @c dir, typically on the Low res mesh,
279  /// and, if @c opts.myPixelSamples also is 1, the input @c org and @c dir are
280  /// so modified by the results with @c dir being the surface normal.
281  /// @c tanu and @c tanv are tangent vectors on Low res mesh that correspond
282  /// to each pixel. They are used to jitter the @c org.
283  /// Both vectors can be passed null if @c opts.myPixelSamples is 1.
284  virtual void computeBaking(exint nrays,
285  UT_Vector3 *org,
286  UT_Vector3 *dir,
287  const float *raybias,
288  const float *tfar,
289  const BakingOptions &opts,
290  bool trace_high_res,
292  const UT_StringHolder *bakemesh_tracesets,
293  const UT_Vector3 *tanu = nullptr,
294  const UT_Vector3 *tanv = nullptr,
295  float *ao = nullptr,
296  float *cavity = nullptr,
297  float *curvature = nullptr,
298  float *thickness = nullptr,
299  float *edge = nullptr,
300  UT_Vector3 *rn = nullptr) const = 0;
301 
302  static inline float vop_bias(float base, float bias)
303  {
304  if (base <= 0)
305  return 0;
306  if (base >= 1)
307  return 1;
308  return SYSsafediv(bias, (1 + (SYSsaferecip(base) - 2)*(1-bias)));
309  }
310 
311  struct AttribInfo
312  {
313  void* myData;
318 
319  bool isValid() const
320  { return myData && myEntries > 0; }
321  };
322 
323  struct MeshData
324  {
326  GT_PrimitiveHandle myPrim = nullptr;
327  GT_DataArrayHandle myVertexStorage = nullptr;
328  GT_DataArrayHandle myPStorage = nullptr;
329  GT_DataArrayHandle myNStorage = nullptr;
330  const UT_Vector3i* myIndices = nullptr;
331  const UT_Vector3* myNormals = nullptr;
332  GT_Owner myNormalOwner = GT_OWNER_INVALID;
333  const GT_PrimPolygonMesh *myMesh = nullptr;
334  bool myLeftHanded = false;
335 
336  // 0=no culling, 1=back, 2=front
337  int myCullBackFaceMode = 0;
338  int myVisibilityCullBackFaceMode = 0;
339 
342 
343  // tracesets data
344  UT_StringHolder myIdentifierTraceset;
346  // set of mesh indices corresponding to
347  // tracesets defined by VisibilityTraceset
349  // uniq bit mask for tracesets defined by VisibilityTraceset
353 
355 
356  void clear()
357  {
358  myPrim.reset();
359  myPStorage.reset();
360  myVertexStorage.reset();
361  myNStorage.reset();
362  myAttributes.clear();
363  myNormalOwner = GT_OWNER_INVALID;
364  myNormals = nullptr;
365  myIndices = nullptr;
366  myMesh = nullptr;
367  }
368  };
369 
370 protected:
371 
372  virtual bool doInit(const UT_Array<PrimitiveInfo>& prims,
373  const BakingOptions& opts) = 0;
374 
375  struct TraceSet
376  {
378  // traceset index. idx0 is reserved for all objects
379  int index = 0;
380  // bitmask with only this trace bit set
382  };
383 
384  // traceset
386  // a map from set of mesh indices to unique traceset
387  // one entry is added for all objects
389 
390  const TraceSet* bakeMeshTraceSet(const UT_StringHolder& name) const;
391  const TraceSet& getTheAllTraceset() const;
392  void buildTracesets(UT_Array<MeshData*>& meshes, bool selfset);
393 
394  bool leftHanddedMesh(const GT_PrimPolygonMesh *pmesh) const;
395  const fpreal32* normals(const GT_PrimPolygonMesh *pmesh,
396  GT_DataArrayHandle &handle,
397  exint& count,
398  GT_Owner& owner) const;
399 
400  void getAttribute(
401  const UT_StringHolder& name,
402  const GT_PrimPolygonMesh *pmesh,
403  AttribInfo& attrinfo) const;
404 
405 private:
406  void dumpTracesets(UT_Array<MeshData*>& meshes) const;
407 
408  const Factory &myFactory;
409 };
410 
412 
413 extern "C" {
414  // DSO function called to register tracers
415  SYS_VISIBILITY_EXPORT extern void newGPUTrace();
416 };
417 
418 #endif
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
UT_BitArray mySelfBitArray
Definition: GT_GPUTrace.h:352
bool needSelfTraceset() const
Definition: GT_GPUTrace.h:152
Unsorted map container.
Definition: UT_Map.h:114
A mesh of polygons.
#define SYS_VISIBILITY_EXPORT
bool isValid() const
Definition: GT_GPUTrace.h:319
UT_StringHolder myVisibilityTraceset
Definition: GT_GPUTrace.h:345
UT_Map< UT_Set< int >, TraceSet > myMeshSetToTraceSet
Definition: GT_GPUTrace.h:388
#define GT_API
Definition: GT_API.h:13
int64 exint
Definition: SYS_Types.h:125
float fpreal32
Definition: SYS_Types.h:200
GT_GPUTrace(const Factory &factory)
Definition: GT_GPUTrace.h:215
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
GLfloat f
Definition: glcorearb.h:1926
UT_Array< UT_StringRef > myFacesSet
Definition: GT_GPUTrace.h:78
GT_GPUTrace::GT_GPUTracePtr GT_GPUTracePtr
Definition: GT_GPUTrace.h:411
static const UT_Matrix4T< float > & getIdentityMatrix()
static const UT_StringHolder theEmptyString
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
UT_BitArray myIdentifierBitArray
Definition: GT_GPUTrace.h:350
UT_StringMap< AttribInfo > myAttributes
Definition: GT_GPUTrace.h:354
PrimitiveInfo(const GT_PrimitiveHandle &handle)
Definition: GT_GPUTrace.h:65
GLuint const GLchar * name
Definition: glcorearb.h:786
exint raysPerPoint() const
Definition: GT_GPUTrace.h:135
GT_Owner
Definition: GT_Types.h:90
const UT_StringHolder & label() const
Definition: GT_GPUTrace.h:224
GU_ConstDetailHandle myGUHandle
Definition: GT_GPUTrace.h:74
PrimitiveInfo(const GU_ConstDetailHandle &handle)
Definition: GT_GPUTrace.h:69
UT_UniquePtr< GT_GPUTrace > GT_GPUTracePtr
Definition: GT_GPUTrace.h:31
static float vop_bias(float base, float bias)
Definition: GT_GPUTrace.h:302
GLuint index
Definition: glcorearb.h:786
virtual ~GT_GPUTrace()
Definition: GT_GPUTrace.h:219
#define SYS_FP32_MAX
Definition: SYS_Types.h:225
GT_DataArrayHandle myBuffer
Definition: GT_GPUTrace.h:314
SYS_VISIBILITY_EXPORT void newGPUTrace()
UT_StringMap< UT_Set< int > > myTracesetNameToMeshes
Definition: GT_GPUTrace.h:385
UT_Set< int > myVisiblityMeshes
Definition: GT_GPUTrace.h:348
unsigned int uint
Definition: SYS_Types.h:45
GLint GLsizei count
Definition: glcorearb.h:405
UT_BitArray myVisibilityBitArray
Definition: GT_GPUTrace.h:351