HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GU_PrimVolumeCache.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: GU_PrimVolumeCache.h ( GU Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GU_PrimVolumeCache__
12 #define __GU_PrimVolumeCache__
13 
14 #include "GU_API.h"
15 
16 #include <string.h>
17 #include <UT/UT_Vector3.h>
18 #include <UT/UT_Vector4.h>
19 #include <UT/UT_Array.h>
20 #include <UT/UT_VoxelArray.h>
22 
23 #include <GEO/GEO_PrimVolume.h>
24 
25 #include "GU_Detail.h"
26 #include "GU_DetailHandle.h"
27 #include "GU_DisplayCache.h"
28 
29 class UT_Ramp;
30 class GEO_PrimVolume;
31 class GEO_PrimVDB;
32 
33 // TODO: When deleting GU_DisplayCache, DON'T delete
34 // GU_PrimVolumeCache. Move it to GT_PrimVolumeCache,
35 // along with any functions needed from the superclass.
36 // It's still used by GT_PrimVolume!!!
38 
40 typedef RE_OGLTexture RE_Texture;
41 #ifdef USE_VULKAN
42 class GR_EnvLight;
43 #endif
45 {
46 public:
48  : myOrg(0,0,0), myDir(0,0,0), myCd(0,0,0), myShadow(0.0),
49  myHasAttenuation(false), myIsAmbient(false), myIsInfinite(false),
50  myIsHeadlight(false), myIsEnvironment(false), myIsCone(false),
51  myEnvMap(nullptr)
52 #ifdef USE_VULKAN
53  , myEnvLight(nullptr)
54 #endif
55  {
56  for(int i = 0; i < 8; i++)
57  myAtten[i] = 1;
58  }
59 
60  bool operator==(const GU_PrimVolumeCacheLight &l) const
61  {
62  // ignore headlight to prevent updates on tumble
63  if (myIsHeadlight &&
64  myIsHeadlight == l.myIsHeadlight)
65  return true;
66 
67  // Ensure attenuation parameters are the same...
68  if(myHasAttenuation != l.myHasAttenuation || (myHasAttenuation &&
69  (myAttenuationCoeffs[0] != l.myAttenuationCoeffs[0]
70  || myAttenuationCoeffs[1] != l.myAttenuationCoeffs[1]
71  || myAttenuationCoeffs[2] != l.myAttenuationCoeffs[2])))
72  return false;
73  // Ensure cone light parameters are the same...
74  if(myIsCone != l.myIsCone || (myIsCone &&
75  (myConeAngle != l.myConeAngle
76  || myConeDelta != l.myConeDelta
77  || myConeRolloff != l.myConeRolloff)))
78  return false;
79 
80  return (myOrg == l.myOrg) &&
81  (myDir == l.myDir) &&
82  (myCd == l.myCd) &&
83  (myIsAmbient == l.myIsAmbient) &&
84  (myIsInfinite == l.myIsInfinite) &&
85  (myShadow == l.myShadow) &&
86  (myIsEnvironment == l.myIsEnvironment) &&
87  (!myIsEnvironment || (myEnvMap == l.myEnvMap &&
88  myOrientation == l.myOrientation
89 #ifdef USE_VULKAN
90  && myEnvLight == l.myEnvLight));
91 #else
92  ));
93 #endif
94  }
95 
96  void setOrg(const UT_Vector3 &org) { myOrg = org; }
97  void setDir(const UT_Vector3 &dir) { myDir = dir; }
98  void setCd(const UT_Vector3 &cd) { myCd = cd; }
99  void setShadowIntensity(float shadow) { myShadow = shadow; }
100  void setIsAmbient(bool isamb) { myIsAmbient = isamb; }
101  void setIsInfinite(bool isinf) { myIsInfinite = isinf; }
102  void setIsHeadlight(bool hl) { myIsHeadlight = hl; }
103  void setIsEnvironment(bool env) { myIsEnvironment = env; }
104  void setEnvironmentMap(const RE_Texture* env) { myEnvMap = env; }
105  void setOrientation(const UT_Matrix3& mat) { myOrientation = mat; }
106  void setIsCone(bool cone) { myIsCone = cone; }
107  void setConeAngle(float angle) { myConeAngle = angle; }
108  void setConeDelta(float delta) { myConeDelta = delta; }
109  void setConeRolloff(float rolloff) { myConeRolloff = rolloff; }
110 #ifdef USE_VULKAN
111  void setEnvironmentLight(GR_EnvLight* env) { myEnvLight = env; }
112 #endif
113 
114  const UT_Vector3 &getOrg() const { return myOrg; }
115  const UT_Vector3 &getDir() const { return myDir; }
116  const UT_Vector3 &getCd() const { return myCd; }
117  float getShadow() const { return myShadow; }
118  float getConeAngle() const { return myConeAngle; }
119  float getConeDelta() const { return myConeDelta; }
120  float getConeRolloff() const { return myConeRolloff; }
121  const RE_Texture* getEnvironmentMap() const { return myEnvMap; }
122  const UT_Matrix3 &getOrientation() const { return myOrientation; }
123 #ifdef USE_VULKAN
124  GR_EnvLight* getEnvironmentLight() const { return myEnvLight; }
125 #endif
126 
127  bool isAmbient() const { return myIsAmbient; }
128  bool isInfinite() const { return myIsInfinite; }
129  bool isHeadlight() const { return myIsHeadlight; }
130  bool isEnvironment() const { return myIsEnvironment; }
131  bool isCone() const { return myIsCone; }
132 
133  float getDistance(const UT_Vector3 &pos) const;
134  // Inv Brightness attenuation for the 8 corners: x, then y, then z; - then +
135  // interpolate, then invert.
136  void setCornerAttenuation(float a[8])
137  {
138  myHasAttenuation = true;
139  for(int i=0; i<8; i++)
140  myAtten[i] = a[i];
141  }
142  bool hasAttenuation() const { return myHasAttenuation; }
143  float getAttenuation(const UT_Vector3F &uvw) const;
144  const float* getCornerAttenuation() const { return myAtten; }
145 
146  float* getAttenuationCoeffArray() { return myAttenuationCoeffs; }
147  const float* getConstAttenuationCoeffArray() const { return myAttenuationCoeffs; }
148 
149 protected:
152  float myShadow;
154  float myAtten[8];
155  float myAttenuationCoeffs[3];
156  bool myIsAmbient, myIsInfinite, myIsHeadlight;
158  bool myIsCone;
159  float myConeAngle, myConeDelta, myConeRolloff;
160 
161  const RE_Texture* myEnvMap;
163 #ifdef USE_VULKAN
164  GR_EnvLight* myEnvLight;
165 #endif
166 };
167 
168 
170 class GU_PrimVolume;
171 
173 {
174 public:
175  SYS_DEPRECATED_HDK(13.0)
177  SYS_DEPRECATED_HDK(13.0)
178  virtual ~GU_PrimVolumeTexture() {}
179 
180  SYS_DEPRECATED_HDK(13.0)
181  virtual void refresh(const UT_VoxelArrayV4 *voxels) = 0;
182  SYS_DEPRECATED_HDK(13.0)
183  virtual int64 getMemoryUsage(bool inclusive) const = 0;
184 };
185 
187 {
188 public:
189  GU_PrimVolumeCacheRamp(UT_Ramp *ramp, bool periodic);
190  ~GU_PrimVolumeCacheRamp();
191 
192  void eval(float &rval, float val) const;
193  void eval(UT_Vector4 &rval, float val) const;
194 
195 protected:
198 };
199 
201 {
202 public:
203  GU_PrimVolumeCacheSampler(const GEO_Detail *gdp, const GEO_Primitive *vol);
205 
206  GU_PrimVolumeCacheRamp *buildRampFromAttribute(
207  const GEO_Detail *gdp,
208  const char *attribname) const;
209  int findCollationIndex(const GEO_Detail *gdp,
210  const GEO_Primitive *vol) const;
211  const GEO_Primitive *selectVolumeFromAttribute(
212  const GEO_Detail *gdp,
213  const char *attribname,
214  int collateidx) const;
215  void selectVolumesFromAttribute(
216  const GEO_Primitive *vol[3],
217  const GEO_Detail *gdp,
218  const char *attribname,
219  int collateidx) const;
220  void getRangeFromAttribute(
221  const GEO_Detail *gdp,
222  const char *attribname,
223  float &rmin, float &rscale) const;
224  float getFloatFromAttribute(
225  const GEO_Detail *gdp,
226  const char *attribname,
227  float def) const;
228 
229  float getDensity(int x, int y, int z) const;
230  UT_Vector4 getDiffuse(int x, int y, int z) const;
231 
232  void getDensityProbe(UT_VoxelProbeF &probe) const;
233  void getEmitProbe(UT_VoxelProbeF &probe) const;
234  void getEmitCdProbe(UT_VoxelProbeV4 &probe) const;
235  const UT_VoxelArrayF *getDensityArray() const;
236 
237  const GEO_Primitive *densityField() const { return myDensityField; }
238  void setDensityField(const GEO_Primitive *densityfield);
239 
240  void hardenFields(const UT_VoxelArray<UT_Vector4> &refvol,
241  const GEO_PrimVolumeXform &volxform,
242  bool isheightfield);
243 
244  static UT_VoxelArrayReadHandleF hardenScalar(
245  const UT_VoxelArrayV4 &refvol,
246  const GEO_PrimVolumeXform &volxform,
247  const GEO_Primitive *field,
249  float rmin, float rinvscale);
250  static UT_VoxelArrayReadHandleV4 hardenVector(
251  const UT_VoxelArrayV4 &refvol,
252  const GEO_PrimVolumeXform &volxform,
253  const GEO_Primitive *field,
255  float rmin, float rinvscale);
256  static UT_VoxelArrayReadHandleV4 hardenVectorMulti(
257  const UT_VoxelArrayV4 &refvol,
258  const GEO_PrimVolumeXform &volxform,
259  const GEO_Primitive *field[3],
261  float rmin, float rinvscale);
262  static UT_VoxelArrayReadHandleV4 hardenVDBVector(
263  const UT_VoxelArrayV4 &refvol,
264  const GEO_PrimVolumeXform &volxform,
265  const GEO_Primitive *field,
267  float rmin, float rinvscale);
268 
270  dst->numTiles() > 1,
271  applyAmbientLight,
272  UT_VoxelArrayV4 *, dst,
273  const UT_Vector4, cd)
274  void applyAmbientLightPartial(UT_VoxelArrayV4 *dst,
275  const UT_Vector4 &cd,
276  const UT_JobInfo &info) const;
277 
278  bool hasEmission() const;
279  float getEmissionScale() const { return myEmissionScale; }
280  float getShadowScale() const { return myShadowScale; }
281 
282 protected:
283  float myDensityMin, myDensityInvRange;
287 
288 
289  float myDiffuseMin, myDiffuseInvRange;
290  const GEO_Primitive *myDiffuseField[3];
292 
294  float myEmissionMin, myEmissionInvRange;
297 
298 
299  float myEmissionCdMin, myEmissionCdInvRange;
300  const GEO_Primitive *myEmissionCdField[3];
302 
307 };
308 
310 {
311 public:
313  ~GU_PrimVolumeCache() override;
314 
315  // Build the cache.
316  int refresh(const GEO_Primitive *parent_prim,
318 
319  // Get the cached data.
320  UT_VoxelArrayV4 *getShadedVoxels(bool *reduced=nullptr) const
321  {
322  if(reduced)
323  *reduced = myVolumeReduced;
324  return myVoxels;
325  }
326  SYS_DEPRECATED_HDK(13.0)
327  GU_PrimVolumeTexture *getTexture() const { return myTexture; }
328  SYS_DEPRECATED_HDK(13.0)
329  void setTexture(GU_PrimVolumeTexture *tex)
330  { myTexture = tex; }
331 
332  GU_ConstDetailHandle getIsoSurface() const { return myIsoSurf; }
333 
334  UT_Vector3 getIsoCd() const { return myIsoCd; }
335 
336  /// The cached resolution
337  UT_Vector3I getRes() const { return myCacheRes; }
338 
340  { return myVolumeXform; }
341 
342  int64 getMemoryUsage(bool inclusive) const
343  {
344  int64 mem = inclusive ? sizeof(*this) : 0;
345  if (myVoxels)
346  mem += myVoxels->getMemoryUsage(true);
347  mem += myLightList.getMemoryUsage(true);
348  if (myTexture)
349  mem += myTexture->getMemoryUsage(true);
350  mem += myIsoSurf.getMemoryUsage(false);
351  return mem;
352  }
353 
354 protected:
355  void lightVoxelsFromLight(const GU_PrimVolumeCacheSampler &sampler,
356  const GU_PrimVolumeCacheLight &light,
357  const UT_Vector3 &cd,
358  float shadowdensity);
359 
360  void rainbowVoxels(const GU_PrimVolumeCacheSampler &sampler);
361 
362  THREADED_METHOD3(GU_PrimVolumeCache, myVoxels->numTiles() > 1,
363  computeAlphaVoxels,
365  float, lightdensity,
366  bool, premultiply)
367  void computeAlphaVoxelsPartial(
369  float lightdensity,
370  bool premultiply,
371  const UT_JobInfo &info);
372 
373  /// Maximum per-axis resolution to do the self shadowing calculations
374  /// at.
375  int getMaxRes(const GEO_Primitive *volume,
376  const GU_PrimVolumeCacheParms &parms,
377  int axis) const;
378 
379  /// Used to convert to and from our *cached* indices, not the
380  /// actual primitive indices that may be a different resolution
381  bool indexToPos(int x, int y, int z, UT_Vector3 &pos) const;
382  bool posToIndex(UT_Vector3 pos, int &x, int &y, int &z) const;
383 
386  SYS_DEPRECATED_HDK(13.0)
388  GU_DetailHandle myIsoSurf;
389  float myOldLOD;
390  UT_Vector3 myIsoCd;
391 
392  UT_Vector3I myCacheRes;
393  GEO_PrimVolumeXform myVolumeXform;
394  bool myVolumeReduced;
395 
396  // Ignore these
397  UT_Matrix4 myCacheXform;
398  UT_Matrix4 myCacheIXform;
399 
400  const UT_VoxelArrayV4 *myCacheVolume;
401 };
402 
404 {
405 public:
407  : myMaxResW(0), myMaxResH(0), myMaxResD(0)
408  , myMax2DRes(0)
409  , myDownscale(1.0)
410  , myIsoPoints(false)
411  , myPreMultiply(true)
412  , myCachedFlag(true)
413  {}
415 
417  { myLightList = lightlist; }
419  { return myLightList; }
420 
421  void setMaxResolution(int w, int h, int d)
422  { myMaxResW = w; myMaxResH = h; myMaxResD = d; }
423  void getMaxResolution(int &w, int &h, int &d) const
424  { w = myMaxResW; h = myMaxResH; d = myMaxResD; }
425 
427  { myDownscale = scale; }
429  { return myDownscale; }
430 
431  void setMax2DRes(int s)
432  { myMax2DRes = s; }
433  int getMax2DRes() const
434  { return myMax2DRes; }
435 
436  void setIsoPoints(bool iso_pts) { myIsoPoints = iso_pts; }
437  bool isIsoPoints() const { return myIsoPoints; }
438 
439  void setPreMultiply(bool premultiply) { myPreMultiply = premultiply; }
440  bool getPreMultiply() const { return myPreMultiply; }
441 
442  void setCached(bool b) const { myCachedFlag = b; }
443  bool isCached() const { return myCachedFlag; }
444 
445 private:
447  int myMaxResW;
448  int myMaxResH;
449  int myMaxResD;
450  int myMax2DRes;
451  fpreal myDownscale;
452  bool myIsoPoints;
453  bool myPreMultiply;
454  mutable bool myCachedFlag;
455 };
456 
458 
459 #endif
460 
const float * getConstAttenuationCoeffArray() const
const UT_Array< GU_PrimVolumeCacheLight > & getLights() const
void setIsAmbient(bool isamb)
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
const UT_Vector3 & getOrg() const
SIM_API const UT_StringHolder angle
void
Definition: png.h:1083
#define SYS_DEPRECATED_PUSH_DISABLE()
void setCd(const UT_Vector3 &cd)
const GEO_Primitive * myEmissionField
#define SYS_DEPRECATED_POP_DISABLE()
void getMaxResolution(int &w, int &h, int &d) const
const RE_Texture * myEnvMap
void setDir(const UT_Vector3 &dir)
void setDownscale(fpreal scale)
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
const RE_Texture * getEnvironmentMap() const
void setLights(UT_Array< GU_PrimVolumeCacheLight > &lightlist)
void setShadowIntensity(float shadow)
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
void setOrientation(const UT_Matrix3 &mat)
void setPreMultiply(bool premultiply)
void setOrg(const UT_Vector3 &org)
GU_PrimVolumeCacheRamp * myEmissionCdRamp
GLint y
Definition: glcorearb.h:103
GLuint sampler
Definition: glcorearb.h:1656
void setEnvironmentMap(const RE_Texture *env)
GU_PrimVolumeCacheRamp * myEmissionRamp
const UT_Matrix3 & getOrientation() const
OutGridT const XformOp bool bool
GU_ConstDetailHandle getIsoSurface() const
#define THREADED_METHOD3(CLASSNAME, DOMULTI, METHOD, PARMTYPE1, PARMNAME1, PARMTYPE2, PARMNAME2, PARMTYPE3, PARMNAME3)
UT_VoxelArrayReadHandleF myEmissionHandle
UT_VoxelArrayV4 * getShadedVoxels(bool *reduced=nullptr) const
GA_API const UT_StringHolder scale
UT_Vector4T< float > UT_Vector4
const UT_Vector3 & getCd() const
GU_PrimVolumeCacheRamp * myDensityRamp
void setConeRolloff(float rolloff)
void setMaxResolution(int w, int h, int d)
void setConeDelta(float delta)
long long int64
Definition: SYS_Types.h:116
const float * getCornerAttenuation() const
HUSD_API bool eval(VtValue &val, T &ret_val)
#define GU_API
Definition: GU_API.h:14
#define SYS_DEPRECATED_HDK(__V__)
void setIsInfinite(bool isinf)
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
#define THREADED_METHOD2_CONST(CLASSNAME, DOMULTI, METHOD, PARMTYPE1, PARMNAME1, PARMTYPE2, PARMNAME2)
UT_Array< GU_PrimVolumeCacheLight > myLightList
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
GLenum GLenum dst
Definition: glcorearb.h:1793
GA_API const UT_StringHolder parms
IMATH_NAMESPACE::V2f IMATH_NAMESPACE::Box2i std::string this attribute is obsolete as of OpenEXR v3 float
UT_Vector3 getIsoCd() const
fpreal64 fpreal
Definition: SYS_Types.h:283
Utility class for containing a color ramp.
Definition: UT_Ramp.h:96
const GEO_Primitive * myDensityField
bool operator==(const GU_PrimVolumeCacheLight &l) const
void setIsoPoints(bool iso_pts)
GLuint GLfloat * val
Definition: glcorearb.h:1608
const GEO_Primitive * densityField() const
void setCornerAttenuation(float a[8])
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
UT_VoxelArrayV4 * myVoxels
UT_VoxelArrayReadHandleV4 myDiffuseHandle
GU_PrimVolumeCacheRamp * myDiffuseRamp
void setCached(bool b) const
UT_Vector3I getRes() const
The cached resolution.
UT_VoxelArrayReadHandleF myDensityHandle
UT_VoxelArrayReadHandleV4 myEmissionCdHandle
Environment light. Infinitely far away incoming rays from all directions.
GEO_PrimVolumeXform getSpaceTransform() const
int64 getMemoryUsage(bool inclusive) const
const UT_Vector3 & getDir() const
void setConeAngle(float angle)