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  GT_TraceDirectionMode myTraceDirection = GTDM_Both;
199  int myPixelSamples = 1;
200  };
201 
202  /// Allocate a tracer from a particular factory
203  static GT_GPUTracePtr allocTrace(const UT_StringRef &name);
204 
205  /// @{
206  /// Allocate the best tracer for a given piece of geometry
207  static GT_GPUTracePtr allocTrace(
208  const UT_Array<PrimitiveInfo>& prims,
209  const UT_StringRef& name,
210  const BakingOptions& opts);
211  /// @}
212 
213  GT_GPUTrace(const Factory &factory)
214  : myFactory(factory)
215  {
216  };
217  virtual ~GT_GPUTrace() {};
218 
220 
221  const UT_StringHolder &name() const { return myFactory.name(); }
222  const UT_StringHolder &label() const { return myFactory.label(); }
223 
224  enum DataType
225  {
226  DT_Int = 0,
231 
232  DT_NumDataType
233  };
234 
236  {
237  // Attribute name.
239  // Output buffer to write.
241  // Type of the buffer
243  };
244 
246  {
247  BCT_Ao, // Ambient Occlusion
248  BCT_Cv, // Cavity
249  BCT_Cu_N, // Curvature (convex)
250  BCT_Cu_P, // Curvature (concave)
251  BCT_Th, // Thickness
252  BCT_Eg, // Edge
253  BCT_Rn, // Rounded Edge Normal
254 
255  BakingCalcType_Num
256  };
257 
258  // Seeds for various different components
259  static constexpr uint AO_SEED = 0x4c41e0cd;
260  static constexpr uint CV_SEED = 0xf2ee2275;
261  static constexpr uint CU_N_SEED = 0x4b65c61e;
262  static constexpr uint CU_P_SEED = 0x9a4bf788;
263  static constexpr uint TH_SEED = 0x4c910506;
264  static constexpr uint EG_SEED = 0x139fa345;
265  static constexpr uint RN_SEED = 0xe4df7f01;
266  static constexpr uint PIXEL_SEED = 0x73aecd3d;
267 
268  /// @{
269  /// When init fails
270  ///
271  bool init(const UT_Array<PrimitiveInfo>& prims, const BakingOptions& opts);
272  /// @}
273 
274  /// Note the @c dir vectors must be normalized.
275  /// If @c trace_high_res is true, High res mesh info are traced using
276  /// rays with given @c org and @c dir, typically on the Low res mesh,
277  /// and, if @c opts.myPixelSamples also is 1, the input @c org and @c dir are
278  /// so modified by the results with @c dir being the surface normal.
279  /// @c tanu and @c tanv are tangent vectors on Low res mesh that correspond
280  /// to each pixel. They are used to jitter the @c org.
281  /// Both vectors can be passed null if @c opts.myPixelSamples is 1.
282  virtual void computeBaking(exint nrays,
283  UT_Vector3 *org,
284  UT_Vector3 *dir,
285  const float *tfar,
286  const BakingOptions &opts,
287  bool trace_high_res,
289  const UT_StringHolder *bakemesh_tracesets,
290  const UT_Vector3 *tanu = nullptr,
291  const UT_Vector3 *tanv = nullptr,
292  float *ao = nullptr,
293  float *cavity = nullptr,
294  float *curvature = nullptr,
295  float *thickness = nullptr,
296  float *edge = nullptr,
297  UT_Vector3 *rn = nullptr) const = 0;
298 
299  static inline float vop_bias(float base, float bias)
300  {
301  if (base <= 0)
302  return 0;
303  if (base >= 1)
304  return 1;
305  return SYSsafediv(bias, (1 + (SYSsaferecip(base) - 2)*(1-bias)));
306  }
307 
308  struct AttribInfo
309  {
310  void* myData;
315 
316  bool isValid() const
317  { return myData && myEntries > 0; }
318  };
319 
320  struct MeshData
321  {
323  GT_PrimitiveHandle myPrim = nullptr;
324  GT_DataArrayHandle myVertexStorage = nullptr;
325  GT_DataArrayHandle myPStorage = nullptr;
326  GT_DataArrayHandle myNStorage = nullptr;
327  const UT_Vector3i* myIndices = nullptr;
328  const UT_Vector3* myNormals = nullptr;
329  GT_Owner myNormalOwner = GT_OWNER_INVALID;
330  const GT_PrimPolygonMesh *myMesh = nullptr;
331  bool myLeftHanded = false;
332 
333  // 0=no culling, 1=back, 2=front
334  int myCullBackFaceMode = 0;
335  int myVisibilityCullBackFaceMode = 0;
336 
338 
339  // tracesets data
340  UT_StringHolder myIdentifierTraceset;
342  // set of mesh indices corresponding to
343  // tracesets defined by VisibilityTraceset
345  // uniq bit mask for tracesets defined by VisibilityTraceset
349 
351 
352  void clear()
353  {
354  myPrim.reset();
355  myPStorage.reset();
356  myVertexStorage.reset();
357  myNStorage.reset();
358  myAttributes.clear();
359  myNormalOwner = GT_OWNER_INVALID;
360  myNormals = nullptr;
361  myIndices = nullptr;
362  myMesh = nullptr;
363  }
364  };
365 
366 protected:
367 
368  virtual bool doInit(const UT_Array<PrimitiveInfo>& prims,
369  const BakingOptions& opts) = 0;
370 
371  struct TraceSet
372  {
374  // traceset index. idx0 is reserved for all objects
375  int index = 0;
376  // bitmask with only this trace bit set
378  };
379 
380  // traceset
382  // a map from set of mesh indices to unique traceset
383  // one entry is added for all objects
385 
386  const TraceSet* bakeMeshTraceSet(const UT_StringHolder& name) const;
387  const TraceSet& getTheAllTraceset() const;
388  void buildTracesets(UT_Array<MeshData*>& meshes, bool selfset);
389 
390  bool leftHanddedMesh(const GT_PrimPolygonMesh *pmesh) const;
391  const fpreal32* normals(const GT_PrimPolygonMesh *pmesh,
392  GT_DataArrayHandle &handle,
393  exint& count,
394  GT_Owner& owner) const;
395 
396  void getAttribute(
397  const UT_StringHolder& name,
398  const GT_PrimPolygonMesh *pmesh,
399  AttribInfo& attrinfo) const;
400 
401 private:
402  void dumpTracesets(UT_Array<MeshData*>& meshes) const;
403 
404  const Factory &myFactory;
405 };
406 
408 
409 extern "C" {
410  // DSO function called to register tracers
411  SYS_VISIBILITY_EXPORT extern void newGPUTrace();
412 };
413 
414 #endif
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
UT_BitArray mySelfBitArray
Definition: GT_GPUTrace.h:348
bool needSelfTraceset() const
Definition: GT_GPUTrace.h:152
Unsorted map container.
Definition: UT_Map.h:109
A mesh of polygons.
#define SYS_VISIBILITY_EXPORT
bool isValid() const
Definition: GT_GPUTrace.h:316
UT_StringHolder myVisibilityTraceset
Definition: GT_GPUTrace.h:341
UT_Map< UT_Set< int >, TraceSet > myMeshSetToTraceSet
Definition: GT_GPUTrace.h:384
#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:213
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:407
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:346
UT_StringMap< AttribInfo > myAttributes
Definition: GT_GPUTrace.h:350
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:222
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:299
GLuint index
Definition: glcorearb.h:786
virtual ~GT_GPUTrace()
Definition: GT_GPUTrace.h:217
GT_DataArrayHandle myBuffer
Definition: GT_GPUTrace.h:311
SYS_VISIBILITY_EXPORT void newGPUTrace()
UT_StringMap< UT_Set< int > > myTracesetNameToMeshes
Definition: GT_GPUTrace.h:381
UT_Set< int > myVisiblityMeshes
Definition: GT_GPUTrace.h:344
unsigned int uint
Definition: SYS_Types.h:45
GLint GLsizei count
Definition: glcorearb.h:405
UT_BitArray myVisibilityBitArray
Definition: GT_GPUTrace.h:347