[1237] | 1 | #include <stack>
|
---|
| 2 | #include <time.h>
|
---|
| 3 | #include <iomanip>
|
---|
| 4 |
|
---|
| 5 | #include "ViewCell.h"
|
---|
| 6 | #include "Plane3.h"
|
---|
| 7 | #include "HierarchyManager.h"
|
---|
| 8 | #include "Mesh.h"
|
---|
| 9 | #include "common.h"
|
---|
| 10 | #include "Environment.h"
|
---|
| 11 | #include "Polygon3.h"
|
---|
| 12 | #include "Ray.h"
|
---|
| 13 | #include "AxisAlignedBox3.h"
|
---|
| 14 | #include "Exporter.h"
|
---|
| 15 | #include "Plane3.h"
|
---|
| 16 | #include "ViewCellsManager.h"
|
---|
| 17 | #include "Beam.h"
|
---|
| 18 | #include "KdTree.h"
|
---|
[1315] | 19 | #include "IntersectableWrapper.h"
|
---|
[1237] | 20 | #include "VspTree.h"
|
---|
| 21 | #include "OspTree.h"
|
---|
[1259] | 22 | #include "BvHierarchy.h"
|
---|
[1707] | 23 | #include "ViewCell.h"
|
---|
[2094] | 24 | #include "TraversalTree.h"
|
---|
[1237] | 25 |
|
---|
| 26 |
|
---|
| 27 | namespace GtpVisibilityPreprocessor {
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | #define USE_FIXEDPOINT_T 0
|
---|
[1786] | 31 | #define STUPID_METHOD 0
|
---|
[1237] | 32 |
|
---|
[1895] | 33 |
|
---|
| 34 |
|
---|
[1237] | 35 | /*******************************************************************/
|
---|
| 36 | /* class HierarchyManager implementation */
|
---|
| 37 | /*******************************************************************/
|
---|
| 38 |
|
---|
| 39 |
|
---|
[1421] | 40 | HierarchyManager::HierarchyManager(const int objectSpaceSubdivisionType):
|
---|
[1308] | 41 | mObjectSpaceSubdivisionType(objectSpaceSubdivisionType),
|
---|
[1279] | 42 | mOspTree(NULL),
|
---|
[2094] | 43 | mBvHierarchy(NULL),
|
---|
| 44 | mTraversalTree(NULL)
|
---|
[1237] | 45 | {
|
---|
[1308] | 46 | switch(mObjectSpaceSubdivisionType)
|
---|
[1279] | 47 | {
|
---|
| 48 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 49 | mOspTree = new OspTree();
|
---|
| 50 | mOspTree->mVspTree = mVspTree;
|
---|
[1379] | 51 | mOspTree->mHierarchyManager = this;
|
---|
[1279] | 52 | break;
|
---|
| 53 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 54 | mBvHierarchy = new BvHierarchy();
|
---|
[1379] | 55 | mBvHierarchy->mHierarchyManager = this;
|
---|
[1279] | 56 | break;
|
---|
| 57 | default:
|
---|
| 58 | break;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[1379] | 61 | // hierarchy manager links view space partition and object space partition
|
---|
[1421] | 62 | mVspTree = new VspTree();
|
---|
[1379] | 63 | mVspTree->mHierarchyManager = this;
|
---|
| 64 |
|
---|
[1580] | 65 | mViewSpaceSubdivisionType = KD_BASED_VIEWSPACE_SUBDIV;
|
---|
[1288] | 66 | ParseEnvironment();
|
---|
[1237] | 67 | }
|
---|
| 68 |
|
---|
| 69 |
|
---|
[1421] | 70 | HierarchyManager::HierarchyManager(KdTree *kdTree):
|
---|
[1308] | 71 | mObjectSpaceSubdivisionType(KD_BASED_OBJ_SUBDIV),
|
---|
[1279] | 72 | mBvHierarchy(NULL)
|
---|
| 73 | {
|
---|
| 74 | mOspTree = new OspTree(*kdTree);
|
---|
| 75 | mOspTree->mVspTree = mVspTree;
|
---|
| 76 |
|
---|
[1421] | 77 | mVspTree = new VspTree();
|
---|
[1379] | 78 | mVspTree->mHierarchyManager = this;
|
---|
[1279] | 79 |
|
---|
[1580] | 80 | mViewSpaceSubdivisionType = KD_BASED_VIEWSPACE_SUBDIV;
|
---|
[1288] | 81 | ParseEnvironment();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
[1723] | 85 | void HierarchySubdivisionStats::Print(ostream &app) const
|
---|
| 86 | {
|
---|
| 87 | app << "#Pass\n" << 0 << endl
|
---|
| 88 | << "#Splits\n" << mNumSplits << endl
|
---|
| 89 | << "#TotalRenderCost\n" << mTotalRenderCost << endl
|
---|
| 90 | << "#TotalEntriesInPvs\n" << mEntriesInPvs << endl
|
---|
| 91 | << "#Memory\n" << mMemoryCost << endl
|
---|
| 92 | << "#StepsView\n" << mViewSpaceSplits << endl
|
---|
| 93 | << "#StepsObject\n" << mObjectSpaceSplits << endl
|
---|
| 94 | << "#VspOspRatio\n" << VspOspRatio() << endl
|
---|
| 95 | << "#FullMem\n" << mFullMemory << endl
|
---|
| 96 | << "#RenderCostDecrease\n" << mRenderCostDecrease << endl
|
---|
[1744] | 97 | << "#Priority\n" << mPriority << endl
|
---|
[1723] | 98 | << "#FpsPerMb\n" << FpsPerMb() << endl
|
---|
| 99 | << endl;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
[1288] | 103 | void HierarchyManager::ParseEnvironment()
|
---|
| 104 | {
|
---|
| 105 | Environment::GetSingleton()->GetFloatValue(
|
---|
| 106 | "Hierarchy.Termination.minGlobalCostRatio", mTermMinGlobalCostRatio);
|
---|
[2228] | 107 |
|
---|
| 108 | Environment::GetSingleton()->GetFloatValue("Hierarchy.minRenderCost", mMinRenderCost);
|
---|
| 109 |
|
---|
[1288] | 110 | Environment::GetSingleton()->GetIntValue(
|
---|
| 111 | "Hierarchy.Termination.globalCostMissTolerance", mTermGlobalCostMissTolerance);
|
---|
| 112 |
|
---|
[1370] | 113 | Environment::GetSingleton()->GetBoolValue(
|
---|
| 114 | "Hierarchy.Construction.startWithObjectSpace", mStartWithObjectSpace);
|
---|
| 115 |
|
---|
[1293] | 116 | Environment::GetSingleton()->GetIntValue(
|
---|
[1294] | 117 | "Hierarchy.Termination.maxLeaves", mTermMaxLeaves);
|
---|
| 118 |
|
---|
| 119 | Environment::GetSingleton()->GetIntValue(
|
---|
[1293] | 120 | "Hierarchy.Construction.type", mConstructionType);
|
---|
| 121 |
|
---|
[1311] | 122 | Environment::GetSingleton()->GetIntValue(
|
---|
| 123 | "Hierarchy.Construction.minDepthForOsp", mMinDepthForObjectSpaceSubdivion);
|
---|
[1370] | 124 |
|
---|
| 125 | Environment::GetSingleton()->GetIntValue(
|
---|
| 126 | "Hierarchy.Construction.minDepthForVsp", mMinDepthForViewSpaceSubdivion);
|
---|
[1311] | 127 |
|
---|
[1314] | 128 | Environment::GetSingleton()->GetBoolValue(
|
---|
| 129 | "Hierarchy.Construction.repairQueue", mRepairQueue);
|
---|
| 130 |
|
---|
[1449] | 131 | Environment::GetSingleton()->GetBoolValue(
|
---|
[1564] | 132 | "Hierarchy.Construction.useMultiLevel", mUseMultiLevelConstruction);
|
---|
[1449] | 133 |
|
---|
[1580] | 134 | Environment::GetSingleton()->GetIntValue(
|
---|
| 135 | "Hierarchy.Construction.levels", mNumMultiLevels);
|
---|
| 136 |
|
---|
[1640] | 137 | Environment::GetSingleton()->GetIntValue(
|
---|
| 138 | "Hierarchy.Construction.minStepsOfSameType", mMinStepsOfSameType);
|
---|
| 139 |
|
---|
[1684] | 140 | Environment::GetSingleton()->GetIntValue(
|
---|
| 141 | "Hierarchy.Construction.maxStepsOfSameType", mMaxStepsOfSameType);
|
---|
| 142 |
|
---|
[1624] | 143 | char subdivisionStatsLog[100];
|
---|
| 144 | Environment::GetSingleton()->GetStringValue("Hierarchy.subdivisionStats", subdivisionStatsLog);
|
---|
| 145 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
| 146 |
|
---|
[1633] | 147 | Environment::GetSingleton()->GetBoolValue(
|
---|
| 148 | "Hierarchy.Construction.recomputeSplitPlaneOnRepair", mRecomputeSplitPlaneOnRepair);
|
---|
[1624] | 149 |
|
---|
[1662] | 150 | Environment::GetSingleton()->GetBoolValue(
|
---|
| 151 | "Hierarchy.Construction.considerMemory", mConsiderMemory);
|
---|
| 152 |
|
---|
[1649] | 153 | Environment::GetSingleton()->GetFloatValue(
|
---|
| 154 | "Hierarchy.Termination.maxMemory", mTermMaxMemory);
|
---|
| 155 |
|
---|
[1727] | 156 | Environment::GetSingleton()->GetIntValue(
|
---|
| 157 | "Hierarchy.Construction.maxRepairs", mMaxRepairs);
|
---|
[1736] | 158 |
|
---|
[1895] | 159 | Environment::GetSingleton()->GetFloatValue(
|
---|
[2227] | 160 | "Hierarchy.Construction.maxAvgRaysPerObject", mMaxAvgRaysPerObject);
|
---|
[1895] | 161 |
|
---|
[1912] | 162 | Environment::GetSingleton()->GetFloatValue(
|
---|
[2227] | 163 | "Hierarchy.Construction.minAvgRaysPerObject", mMinAvgRaysPerObject);
|
---|
[1912] | 164 |
|
---|
[2130] | 165 | Environment::GetSingleton()->GetBoolValue(
|
---|
| 166 | "Hierarchy.useTraversalTree", mUseTraversalTree);
|
---|
| 167 |
|
---|
[1705] | 168 | Debug << "******** Hierarchy Manager Options ***********" << endl;
|
---|
[1294] | 169 | Debug << "max leaves: " << mTermMaxLeaves << endl;
|
---|
[1288] | 170 | Debug << "min global cost ratio: " << mTermMinGlobalCostRatio << endl;
|
---|
| 171 | Debug << "global cost miss tolerance: " << mTermGlobalCostMissTolerance << endl;
|
---|
[1314] | 172 | Debug << "min depth for object space subdivision: " << mMinDepthForObjectSpaceSubdivion << endl;
|
---|
| 173 | Debug << "repair queue: " << mRepairQueue << endl;
|
---|
[1580] | 174 | Debug << "number of multilevels: " << mNumMultiLevels << endl;
|
---|
[1633] | 175 | Debug << "recompute split plane on repair: " << mRecomputeSplitPlaneOnRepair << endl;
|
---|
[1640] | 176 | Debug << "minimal number of steps from same type: " << mMinStepsOfSameType << endl;
|
---|
[1649] | 177 | Debug << "maximal allowed memory: " << mTermMaxMemory << endl;
|
---|
[1673] | 178 | Debug << "consider memory: " << mConsiderMemory << endl;
|
---|
[1684] | 179 | Debug << "min steps of same kind: " << mMinStepsOfSameType << endl;
|
---|
| 180 | Debug << "max steps of same kind: " << mMaxStepsOfSameType << endl;
|
---|
[1727] | 181 | Debug << "max repairs: " << mMaxRepairs << endl;
|
---|
[2227] | 182 | Debug << "max avg rays per object: " << mMaxAvgRaysPerObject << endl;
|
---|
| 183 | Debug << "mín avg rays per object: " << mMinAvgRaysPerObject << endl;
|
---|
[2228] | 184 | Debug << "mín render cost: " << mMinRenderCost << endl;
|
---|
[1632] | 185 |
|
---|
[2199] | 186 | // for comparing it with byte - value
|
---|
| 187 | mTermMaxMemory *= (1024.0f * 1024.0f);
|
---|
[1895] | 188 |
|
---|
[1632] | 189 | switch (mConstructionType)
|
---|
| 190 | {
|
---|
| 191 | case 0:
|
---|
| 192 | Debug << "construction type: sequential" << endl;
|
---|
| 193 | break;
|
---|
| 194 | case 1:
|
---|
| 195 | Debug << "construction type: interleaved" << endl;
|
---|
| 196 | break;
|
---|
| 197 | case 2:
|
---|
| 198 | Debug << "construction type: gradient" << endl;
|
---|
| 199 | break;
|
---|
| 200 | case 3:
|
---|
| 201 | Debug << "construction type: multilevel" << endl;
|
---|
| 202 | break;
|
---|
| 203 | default:
|
---|
| 204 | Debug << "construction type " << mConstructionType << " unknown" << endl;
|
---|
| 205 | break;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[1625] | 208 | //Debug << "min render cost " << mMinRenderCostDecrease << endl;
|
---|
[1449] | 209 | Debug << endl;
|
---|
[1279] | 210 | }
|
---|
| 211 |
|
---|
| 212 |
|
---|
| 213 | HierarchyManager::~HierarchyManager()
|
---|
| 214 | {
|
---|
| 215 | DEL_PTR(mOspTree);
|
---|
[1421] | 216 | DEL_PTR(mVspTree);
|
---|
[1279] | 217 | DEL_PTR(mBvHierarchy);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 |
|
---|
[1370] | 221 | int HierarchyManager::GetObjectSpaceSubdivisionType() const
|
---|
| 222 | {
|
---|
| 223 | return mObjectSpaceSubdivisionType;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | int HierarchyManager::GetViewSpaceSubdivisionType() const
|
---|
| 228 | {
|
---|
| 229 | return mViewSpaceSubdivisionType;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 |
|
---|
[1279] | 233 | void HierarchyManager::SetViewCellsManager(ViewCellsManager *vcm)
|
---|
| 234 | {
|
---|
| 235 | mVspTree->SetViewCellsManager(vcm);
|
---|
| 236 |
|
---|
| 237 | if (mOspTree)
|
---|
[1576] | 238 | {
|
---|
[1279] | 239 | mOspTree->SetViewCellsManager(vcm);
|
---|
[1576] | 240 | }
|
---|
[1416] | 241 | else if (mBvHierarchy)
|
---|
[1576] | 242 | {
|
---|
[1279] | 243 | mBvHierarchy->SetViewCellsManager(vcm);
|
---|
[1576] | 244 | }
|
---|
[1279] | 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | void HierarchyManager::SetViewCellsTree(ViewCellsTree *vcTree)
|
---|
| 249 | {
|
---|
| 250 | mVspTree->SetViewCellsTree(vcTree);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 |
|
---|
[1379] | 254 | VspTree *HierarchyManager::GetVspTree()
|
---|
| 255 | {
|
---|
| 256 | return mVspTree;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[1563] | 259 | /*
|
---|
[1379] | 260 | AxisAlignedBox3 HierarchyManager::GetViewSpaceBox() const
|
---|
| 261 | {
|
---|
| 262 | return mVspTree->mBoundingBox;
|
---|
[1563] | 263 | }*/
|
---|
[1379] | 264 |
|
---|
| 265 |
|
---|
[1416] | 266 | AxisAlignedBox3 HierarchyManager::GetObjectSpaceBox() const
|
---|
| 267 | {
|
---|
| 268 | switch (mObjectSpaceSubdivisionType)
|
---|
| 269 | {
|
---|
| 270 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 271 | return mOspTree->mBoundingBox;
|
---|
| 272 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 273 | return mBvHierarchy->mBoundingBox;
|
---|
| 274 | default:
|
---|
[1576] | 275 | // hack: empty box
|
---|
[1416] | 276 | return AxisAlignedBox3();
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 |
|
---|
[1625] | 281 | SubdivisionCandidate *HierarchyManager::NextSubdivisionCandidate(SplitQueue &splitQueue)
|
---|
[1237] | 282 | {
|
---|
[1625] | 283 | SubdivisionCandidate *splitCandidate = splitQueue.Top();
|
---|
| 284 | splitQueue.Pop();
|
---|
[1237] | 285 |
|
---|
[1733] | 286 | // split was not reevaluated before => do it now
|
---|
| 287 | if (splitCandidate->IsDirty())
|
---|
[2198] | 288 | {
|
---|
[1733] | 289 | splitCandidate->EvalCandidate();
|
---|
[2198] | 290 | }
|
---|
[1733] | 291 |
|
---|
[1237] | 292 | return splitCandidate;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 |
|
---|
[1830] | 296 | float HierarchyManager::EvalFullMem() const
|
---|
| 297 | {
|
---|
| 298 | // question: should I also add the mem usage of the hierarchies?
|
---|
| 299 | const float objectSpaceMem = 16;//GetObjectSpaceMemUsage();
|
---|
| 300 | const float viewSpaceMem = 16;//mVspTree->GetMemUsage();
|
---|
| 301 | // HACK: the same value is used for view and object space
|
---|
| 302 | return mHierarchyStats.mMemory + mHierarchyStats.Leaves() * objectSpaceMem;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 |
|
---|
[1624] | 306 | void HierarchyManager::EvalSubdivisionStats()
|
---|
[1237] | 307 | {
|
---|
[1830] | 308 |
|
---|
[1723] | 309 | HierarchySubdivisionStats stats;
|
---|
[1744] | 310 |
|
---|
[1723] | 311 | stats.mNumSplits = mHierarchyStats.Leaves();
|
---|
| 312 | stats.mTotalRenderCost = mHierarchyStats.mTotalCost;
|
---|
| 313 | stats.mEntriesInPvs = mHierarchyStats.mPvsEntries;
|
---|
| 314 | stats.mMemoryCost = mHierarchyStats.mMemory / float(1024 * 1024);
|
---|
[1830] | 315 | stats.mFullMemory = EvalFullMem() / float(1024 * 1024);
|
---|
[1723] | 316 | stats.mViewSpaceSplits = mVspTree->mVspStats.Leaves();
|
---|
| 317 | stats.mObjectSpaceSplits = GetObjectSpaceSubdivisionLeaves();
|
---|
[1744] | 318 | stats.mRenderCostDecrease = mHierarchyStats.mRenderCostDecrease;
|
---|
| 319 | stats.mPriority = mPriority;
|
---|
| 320 |
|
---|
[1723] | 321 | stats.Print(mSubdivisionStats);
|
---|
[1237] | 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
| 325 | void HierarchyManager::AddSubdivisionStats(const int splits,
|
---|
| 326 | const float renderCostDecr,
|
---|
[1576] | 327 | const float totalRenderCost,
|
---|
[1625] | 328 | const int pvsEntries,
|
---|
[1640] | 329 | const float memory,
|
---|
[1654] | 330 | const float renderCostPerStorage,
|
---|
| 331 | const float vspOspRatio)
|
---|
[1237] | 332 | {
|
---|
[1308] | 333 | mSubdivisionStats
|
---|
[1237] | 334 | << "#Splits\n" << splits << endl
|
---|
| 335 | << "#RenderCostDecrease\n" << renderCostDecr << endl
|
---|
[1577] | 336 | << "#TotalEntriesInPvs\n" << pvsEntries << endl
|
---|
[1625] | 337 | << "#TotalRenderCost\n" << totalRenderCost << endl
|
---|
| 338 | << "#Memory\n" << memory << endl
|
---|
[1660] | 339 | << "#FpsPerMb\n" << renderCostPerStorage << endl
|
---|
[1723] | 340 | << "#VspOspRatio\n" << vspOspRatio << endl
|
---|
| 341 | << endl;
|
---|
[1237] | 342 | }
|
---|
| 343 |
|
---|
| 344 |
|
---|
| 345 | bool HierarchyManager::GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const
|
---|
| 346 | {
|
---|
[1421] | 347 | const bool terminationCriteriaMet =
|
---|
| 348 | (0
|
---|
[1294] | 349 | || (mHierarchyStats.Leaves() >= mTermMaxLeaves)
|
---|
[1830] | 350 | //|| (mHierarchyStats.mMemory >= mTermMaxMemory)
|
---|
| 351 | || (EvalFullMem() >= mTermMaxMemory)
|
---|
[1649] | 352 | || candidate->GlobalTerminationCriteriaMet()
|
---|
| 353 | //|| (mHierarchyStats.mRenderCostDecrease < mMinRenderCostDecrease)
|
---|
[1633] | 354 | //|| (mHierarchyStats.mGlobalCostMisses >= mTermGlobalCostMissTolerance)
|
---|
[1288] | 355 | );
|
---|
[1421] | 356 |
|
---|
[1715] | 357 | #if GTP_DEBUG
|
---|
[1610] | 358 | if (terminationCriteriaMet)
|
---|
[1421] | 359 | {
|
---|
| 360 | Debug << "hierarchy global termination criteria met:" << endl;
|
---|
| 361 | Debug << "leaves: " << mHierarchyStats.Leaves() << " " << mTermMaxLeaves << endl;
|
---|
[1449] | 362 | Debug << "cost misses: " << mHierarchyStats.mGlobalCostMisses << " " << mTermGlobalCostMissTolerance << endl;
|
---|
[1649] | 363 | Debug << "memory: " << mHierarchyStats.mMemory << " " << mTermMaxMemory << endl;
|
---|
[1830] | 364 | Debug << "full memory: " << EvalFullMem() << " " << mTermMaxMemory << endl;
|
---|
[1421] | 365 | }
|
---|
[1653] | 366 | #endif
|
---|
[1610] | 367 |
|
---|
[1421] | 368 | return terminationCriteriaMet;
|
---|
[1237] | 369 | }
|
---|
| 370 |
|
---|
| 371 |
|
---|
| 372 | void HierarchyManager::Construct(const VssRayContainer &sampleRays,
|
---|
[1311] | 373 | const ObjectContainer &objects,
|
---|
| 374 | AxisAlignedBox3 *forcedViewSpace)
|
---|
[1237] | 375 | {
|
---|
[1679] | 376 | mTimeStamp = 1;
|
---|
| 377 |
|
---|
[1627] | 378 | switch (mConstructionType)
|
---|
[1449] | 379 | {
|
---|
[1627] | 380 | case MULTILEVEL:
|
---|
[1449] | 381 | ConstructMultiLevel(sampleRays, objects, forcedViewSpace);
|
---|
[1627] | 382 | break;
|
---|
| 383 | case INTERLEAVED:
|
---|
| 384 | case SEQUENTIAL:
|
---|
[1624] | 385 | ConstructInterleaved(sampleRays, objects, forcedViewSpace);
|
---|
[2206] | 386 | PrintTimings(true);
|
---|
[1627] | 387 | break;
|
---|
| 388 | case GRADIENT:
|
---|
| 389 | ConstructInterleavedWithGradient(sampleRays, objects, forcedViewSpace);
|
---|
[2206] | 390 | PrintTimings(true);
|
---|
[1627] | 391 | break;
|
---|
| 392 | default:
|
---|
| 393 | break;
|
---|
[1624] | 394 | }
|
---|
[1642] | 395 |
|
---|
[2124] | 396 | // hack
|
---|
[1642] | 397 | if (mUseMultiLevelConstruction)
|
---|
| 398 | {
|
---|
| 399 | cout << "starting optimizing multilevel ... " << endl;
|
---|
[2124] | 400 | // try to optimize the hierarchy from above
|
---|
[1642] | 401 | OptimizeMultiLevel(sampleRays, objects, forcedViewSpace);
|
---|
| 402 |
|
---|
| 403 | cout << "finished" << endl;
|
---|
| 404 | }
|
---|
[2093] | 405 |
|
---|
| 406 | if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
|
---|
| 407 | {
|
---|
| 408 | mBvHierarchy->SetUniqueNodeIds();
|
---|
| 409 | }
|
---|
[2094] | 410 |
|
---|
[2124] | 411 | // create a traversal tree which is optimized for view cell casting
|
---|
[2130] | 412 | if (mUseTraversalTree)
|
---|
[2124] | 413 | {
|
---|
| 414 | CreateTraversalTree();
|
---|
| 415 | }
|
---|
[1624] | 416 | }
|
---|
| 417 |
|
---|
| 418 |
|
---|
[2187] | 419 | void HierarchyManager::PrintTimings(const bool lastSplitWasOsp)
|
---|
| 420 | {
|
---|
[2198] | 421 | double sortTime, evalTime, nodeTime, splitTime, subdTime, planeTime, collectTime, viewCellsTime;
|
---|
[2187] | 422 |
|
---|
[2199] | 423 | sortTime = mBvHierarchy->mSortTimer.TotalTime();
|
---|
[2187] | 424 | evalTime = mBvHierarchy->mEvalTimer.TotalTime();
|
---|
| 425 | nodeTime = mBvHierarchy->mNodeTimer.TotalTime();
|
---|
| 426 | splitTime = mBvHierarchy->mSplitTimer.TotalTime();
|
---|
| 427 | subdTime = mBvHierarchy->mSubdivTimer.TotalTime();
|
---|
[2198] | 428 | planeTime = mBvHierarchy->mPlaneTimer.TotalTime();
|
---|
| 429 | collectTime = mBvHierarchy->mCollectTimer.TotalTime();
|
---|
[2187] | 430 |
|
---|
| 431 | cout << "bvh times"
|
---|
| 432 | << " sort : " << sortTime
|
---|
| 433 | << " eval : " << evalTime
|
---|
| 434 | << " node : " << nodeTime
|
---|
| 435 | << " split: " << splitTime
|
---|
[2198] | 436 | << " subd : " << subdTime
|
---|
| 437 | << " plane: " << planeTime
|
---|
| 438 | << " colct: " << collectTime
|
---|
| 439 | << endl;
|
---|
[2187] | 440 |
|
---|
| 441 | Debug << "bvh times"
|
---|
| 442 | << " sort : " << sortTime
|
---|
| 443 | << " eval : " << evalTime
|
---|
| 444 | << " node : " << nodeTime
|
---|
| 445 | << " split: " << splitTime
|
---|
[2198] | 446 | << " subd : " << subdTime
|
---|
| 447 | << " plane: " << planeTime
|
---|
| 448 | << " colct: " << collectTime
|
---|
| 449 | << endl;
|
---|
[2187] | 450 |
|
---|
| 451 | sortTime = mVspTree->mSortTimer.TotalTime();
|
---|
| 452 | evalTime = mVspTree->mEvalTimer.TotalTime();
|
---|
| 453 | nodeTime = mVspTree->mNodeTimer.TotalTime();
|
---|
| 454 | splitTime = mVspTree->mSplitTimer.TotalTime();
|
---|
| 455 | subdTime = mVspTree->mSubdivTimer.TotalTime();
|
---|
[2198] | 456 | planeTime = mVspTree->mPlaneTimer.TotalTime();
|
---|
| 457 | viewCellsTime = mVspTree->mViewCellsTimer.TotalTime();
|
---|
[2187] | 458 |
|
---|
| 459 | cout << "vsp times"
|
---|
| 460 | << " sort : " << sortTime
|
---|
| 461 | << " eval : " << evalTime
|
---|
| 462 | << " node : " << nodeTime
|
---|
| 463 | << " split: " << splitTime
|
---|
[2198] | 464 | << " subd : " << subdTime
|
---|
| 465 | << " plane: " << planeTime
|
---|
| 466 | << " viewc: " << viewCellsTime
|
---|
| 467 | << endl;
|
---|
[2187] | 468 |
|
---|
| 469 | Debug << "vsp times"
|
---|
[2198] | 470 | << " sort : " << sortTime
|
---|
| 471 | << " eval : " << evalTime
|
---|
| 472 | << " node : " << nodeTime
|
---|
| 473 | << " split: " << splitTime
|
---|
| 474 | << " subd : " << subdTime
|
---|
| 475 | << " plane: " << planeTime
|
---|
| 476 | << " viewc: " << viewCellsTime
|
---|
| 477 | << endl;
|
---|
| 478 |
|
---|
[2187] | 479 | cout << endl;
|
---|
| 480 | Debug << endl;
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 |
|
---|
[1686] | 484 | void HierarchyManager::ConstructInterleavedWithGradient(const VssRayContainer &sampleRays,
|
---|
[1626] | 485 | const ObjectContainer &objects,
|
---|
| 486 | AxisAlignedBox3 *forcedViewSpace)
|
---|
[1624] | 487 | {
|
---|
| 488 | mHierarchyStats.Reset();
|
---|
| 489 | mHierarchyStats.Start();
|
---|
[1625] | 490 |
|
---|
[1624] | 491 | mHierarchyStats.mNodes = 2;
|
---|
[1640] | 492 |
|
---|
| 493 | // create first nodes
|
---|
| 494 | mVspTree->Initialise(sampleRays, forcedViewSpace);
|
---|
| 495 | InitialiseObjectSpaceSubdivision(objects);
|
---|
| 496 |
|
---|
| 497 | // hack: assume that object space can be seen from view space
|
---|
[1677] | 498 | mHierarchyStats.mTotalCost = mInitialRenderCost = (float)objects.size();
|
---|
[1667] | 499 | // only one entry for start
|
---|
[1640] | 500 | mHierarchyStats.mPvsEntries = 1;
|
---|
[1667] | 501 | mHierarchyStats.mMemory = (float)ObjectPvs::GetEntrySizeByte();
|
---|
[1640] | 502 |
|
---|
| 503 | EvalSubdivisionStats();
|
---|
[1624] | 504 | Debug << "setting total cost to " << mHierarchyStats.mTotalCost << endl;
|
---|
| 505 |
|
---|
| 506 | const long startTime = GetTime();
|
---|
| 507 | cout << "Constructing view space / object space tree ... \n";
|
---|
| 508 |
|
---|
[1633] | 509 | SplitQueue objectSpaceQueue;
|
---|
| 510 | SplitQueue viewSpaceQueue;
|
---|
| 511 |
|
---|
[1696] | 512 | int vspSteps = 0, ospSteps = 0;
|
---|
[1684] | 513 |
|
---|
[1624] | 514 | // use sah for evaluating osp tree construction
|
---|
| 515 | // in the first iteration of the subdivision
|
---|
| 516 | mSavedViewSpaceSubdivisionType = mViewSpaceSubdivisionType;
|
---|
| 517 | mViewSpaceSubdivisionType = NO_VIEWSPACE_SUBDIV;
|
---|
[1662] | 518 | mSavedObjectSpaceSubdivisionType = mObjectSpaceSubdivisionType;
|
---|
[1624] | 519 |
|
---|
[1632] | 520 | // number of initial splits
|
---|
[1640] | 521 | const int minSteps = mMinStepsOfSameType;
|
---|
[1684] | 522 | const int maxSteps = mMaxStepsOfSameType;
|
---|
[1625] | 523 |
|
---|
[1779] | 524 | PrepareObjectSpaceSubdivision(objectSpaceQueue, sampleRays, objects);
|
---|
| 525 |
|
---|
[1633] | 526 | /////////////////////////
|
---|
| 527 | // calulcate initial object space splits
|
---|
| 528 |
|
---|
[1684] | 529 | SubdivisionCandidateContainer dirtyList;
|
---|
[1633] | 530 |
|
---|
| 531 | // subdivide object space first
|
---|
| 532 | // for first round, use sah splits. Once view space partition
|
---|
| 533 | // has started, use render cost heuristics instead
|
---|
[1727] | 534 | ospSteps = RunConstruction(objectSpaceQueue,
|
---|
| 535 | dirtyList,
|
---|
| 536 | NULL,
|
---|
| 537 | minSteps,
|
---|
| 538 | maxSteps);
|
---|
| 539 |
|
---|
[1640] | 540 | cout << "\n" << ospSteps << " object space partition steps taken" << endl;
|
---|
[1633] | 541 |
|
---|
[2206] | 542 | //PrintTimings(true);
|
---|
[2199] | 543 |
|
---|
| 544 | ///////////////
|
---|
| 545 | //-- create view space
|
---|
[1779] | 546 | PrepareViewSpaceSubdivision(viewSpaceQueue, sampleRays, objects);
|
---|
[1633] | 547 |
|
---|
[1684] | 548 | dirtyList.clear();
|
---|
[1642] | 549 | // view space subdivision started
|
---|
[1633] | 550 | mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
|
---|
| 551 |
|
---|
[1684] | 552 | if (1)
|
---|
| 553 | {
|
---|
| 554 | // rather also start with 100 view space splits to avoid initial bias.
|
---|
| 555 | vspSteps = RunConstruction(viewSpaceQueue, dirtyList, NULL, minSteps, maxSteps);
|
---|
| 556 | cout << "\n" << vspSteps << " view space partition steps taken" << endl;
|
---|
| 557 |
|
---|
| 558 | /// Repair split queue
|
---|
| 559 | cout << "repairing queue ... " << endl;
|
---|
| 560 | RepairQueue(dirtyList, objectSpaceQueue, true);
|
---|
| 561 | cout << "repaired " << (int)dirtyList.size() << " candidates" << endl;
|
---|
[1633] | 562 |
|
---|
[1684] | 563 | dirtyList.clear();
|
---|
[2206] | 564 | //PrintTimings(false);
|
---|
[1684] | 565 | }
|
---|
| 566 | else
|
---|
| 567 | {
|
---|
| 568 | // the priorities were calculated for driving sah.
|
---|
| 569 | // => recalculate "real" priorities taking visibility into
|
---|
| 570 | // account so we can compare to view space splits
|
---|
| 571 | ResetQueue(objectSpaceQueue, false);
|
---|
| 572 | }
|
---|
| 573 |
|
---|
[1624] | 574 | // This method subdivides view space / object space
|
---|
| 575 | // in order to converge to some optimal cost for this partition
|
---|
| 576 | // start with object space partiton
|
---|
| 577 | // then optimizate view space partition for the current osp
|
---|
| 578 | // and vice versa until iteration depth is reached.
|
---|
[1696] | 579 |
|
---|
[1684] | 580 | bool lastSplitWasOsp = true;
|
---|
| 581 |
|
---|
[1633] | 582 | while (!(viewSpaceQueue.Empty() && objectSpaceQueue.Empty()))
|
---|
[1624] | 583 | {
|
---|
[1662] | 584 | // decide upon next split type
|
---|
[1784] | 585 | const float vspPriority =
|
---|
| 586 | viewSpaceQueue.Top() ? viewSpaceQueue.Top()->GetPriority() : -1e20f;
|
---|
| 587 | const float ospPriority =
|
---|
| 588 | objectSpaceQueue.Top() ? objectSpaceQueue.Top()->GetPriority() : -1e20f;
|
---|
[1662] | 589 |
|
---|
[1640] | 590 | cout << "new decicion, vsp: " << vspPriority << ", osp: " << ospPriority << endl;
|
---|
| 591 |
|
---|
[1633] | 592 | // should view or object space be subdivided further?
|
---|
[1684] | 593 | if (ospPriority >= vspPriority)
|
---|
| 594 | //if (!lastSplitWasOsp)
|
---|
[1633] | 595 | {
|
---|
[2187] | 596 | /////////////////
|
---|
| 597 | // subdivide object space with respect to the objects
|
---|
| 598 |
|
---|
[1679] | 599 | lastSplitWasOsp = true;
|
---|
[1673] | 600 | cout << "osp" << endl;
|
---|
[1684] | 601 |
|
---|
[1633] | 602 | // dirtied view space candidates
|
---|
| 603 | SubdivisionCandidateContainer dirtyVspList;
|
---|
[1624] | 604 |
|
---|
[1727] | 605 | // subdivide object space first for first round,
|
---|
| 606 | // use sah splits. Once view space partition
|
---|
[1633] | 607 | // has started, use render cost heuristics instead
|
---|
[1727] | 608 | const int ospSteps = RunConstruction(objectSpaceQueue,
|
---|
| 609 | dirtyVspList,
|
---|
| 610 | viewSpaceQueue.Top(),
|
---|
| 611 | minSteps,
|
---|
| 612 | maxSteps);
|
---|
[1624] | 613 |
|
---|
[1640] | 614 | cout << "\n" << ospSteps << " object space partition steps taken" << endl;
|
---|
[1684] | 615 | Debug << "\n" << ospSteps << " object space partition steps taken" << endl;
|
---|
| 616 |
|
---|
[1640] | 617 | /// Repair split queue, i.e., affected view space candidates
|
---|
[1633] | 618 | cout << "repairing queue ... " << endl;
|
---|
[1736] | 619 | const int repaired = RepairQueue(dirtyVspList, viewSpaceQueue, true);
|
---|
[1727] | 620 |
|
---|
[1784] | 621 | cout << "\nrepaired " << repaired << " candidates from "
|
---|
| 622 | << (int)dirtyVspList.size() << " dirtied candidates" << endl;
|
---|
[1633] | 623 | }
|
---|
| 624 | else
|
---|
| 625 | {
|
---|
| 626 | /////////////////
|
---|
| 627 | // subdivide view space with respect to the objects
|
---|
[1625] | 628 |
|
---|
[2187] | 629 | lastSplitWasOsp = false;
|
---|
| 630 | cout << "vsp" << endl;
|
---|
| 631 |
|
---|
[1684] | 632 | // dirtied object space candidates
|
---|
[1633] | 633 | SubdivisionCandidateContainer dirtyOspList;
|
---|
[1624] | 634 |
|
---|
[1633] | 635 | // process view space candidates
|
---|
[1727] | 636 | const int vspSteps = RunConstruction(viewSpaceQueue,
|
---|
| 637 | dirtyOspList,
|
---|
| 638 | objectSpaceQueue.Top(),
|
---|
| 639 | minSteps,
|
---|
| 640 | maxSteps);
|
---|
[1624] | 641 |
|
---|
[1640] | 642 | cout << "\n" << vspSteps << " view space partition steps taken" << endl;
|
---|
[1684] | 643 | Debug << "\n" << vspSteps << " view space partition steps taken" << endl;
|
---|
[1625] | 644 |
|
---|
[1633] | 645 | // view space subdivision constructed
|
---|
| 646 | mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
|
---|
[1624] | 647 |
|
---|
[1633] | 648 | /// Repair split queue
|
---|
| 649 | cout << "repairing queue ... " << endl;
|
---|
[1736] | 650 | const int repaired = RepairQueue(dirtyOspList, objectSpaceQueue, true);
|
---|
[1727] | 651 |
|
---|
[1784] | 652 | cout << "\nrepaired " << repaired << " candidates from "
|
---|
| 653 | << (int)dirtyOspList.size() << " dirtied candidates" << endl;
|
---|
[1633] | 654 | }
|
---|
[2187] | 655 |
|
---|
[2206] | 656 | //PrintTimings(lastSplitWasOsp);
|
---|
[1449] | 657 | }
|
---|
[1633] | 658 |
|
---|
[1624] | 659 | cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
| 660 |
|
---|
| 661 | mHierarchyStats.Stop();
|
---|
| 662 | mVspTree->mVspStats.Stop();
|
---|
[1625] | 663 |
|
---|
[1649] | 664 | FinishObjectSpaceSubdivision(objects, !mUseMultiLevelConstruction);
|
---|
[1449] | 665 | }
|
---|
| 666 |
|
---|
| 667 |
|
---|
| 668 | void HierarchyManager::ConstructInterleaved(const VssRayContainer &sampleRays,
|
---|
| 669 | const ObjectContainer &objects,
|
---|
| 670 | AxisAlignedBox3 *forcedViewSpace)
|
---|
| 671 | {
|
---|
[1308] | 672 | mHierarchyStats.Reset();
|
---|
| 673 | mHierarchyStats.Start();
|
---|
[1664] | 674 |
|
---|
| 675 | // two nodes for view space and object space
|
---|
| 676 | mHierarchyStats.mNodes = 2;
|
---|
[1640] | 677 | mHierarchyStats.mPvsEntries = 1;
|
---|
[1667] | 678 | mHierarchyStats.mMemory = (float)ObjectPvs::GetEntrySizeByte();
|
---|
[1640] | 679 | mHierarchyStats.mTotalCost = (float)objects.size();
|
---|
[1449] | 680 |
|
---|
[1624] | 681 | mHierarchyStats.mRenderCostDecrease = 0;
|
---|
[1237] | 682 |
|
---|
[1624] | 683 | EvalSubdivisionStats();
|
---|
| 684 | Debug << "setting total cost to " << mHierarchyStats.mTotalCost << endl;
|
---|
| 685 |
|
---|
[1311] | 686 | const long startTime = GetTime();
|
---|
[1308] | 687 | cout << "Constructing view space / object space tree ... \n";
|
---|
[1237] | 688 |
|
---|
[1640] | 689 | // create only roots
|
---|
| 690 | mVspTree->Initialise(sampleRays, forcedViewSpace);
|
---|
| 691 | InitialiseObjectSpaceSubdivision(objects);
|
---|
[1379] | 692 |
|
---|
[1633] | 693 | // use objects for evaluating vsp tree construction in the
|
---|
| 694 | // first levels of the subdivision
|
---|
[1323] | 695 | mSavedObjectSpaceSubdivisionType = mObjectSpaceSubdivisionType;
|
---|
| 696 | mObjectSpaceSubdivisionType = NO_OBJ_SUBDIV;
|
---|
| 697 |
|
---|
[1370] | 698 | mSavedViewSpaceSubdivisionType = mViewSpaceSubdivisionType;
|
---|
| 699 | mViewSpaceSubdivisionType = NO_VIEWSPACE_SUBDIV;
|
---|
| 700 |
|
---|
[1632] | 701 | // start view space subdivison immediately?
|
---|
[1329] | 702 | if (StartViewSpaceSubdivision())
|
---|
| 703 | {
|
---|
[1624] | 704 | // prepare vsp tree for traversal
|
---|
[1640] | 705 | mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
|
---|
[1779] | 706 | PrepareViewSpaceSubdivision(mTQueue, sampleRays, objects);
|
---|
[1329] | 707 | }
|
---|
| 708 |
|
---|
[1323] | 709 | // start object space subdivision immediately?
|
---|
| 710 | if (StartObjectSpaceSubdivision())
|
---|
| 711 | {
|
---|
| 712 | mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
|
---|
[1779] | 713 | PrepareObjectSpaceSubdivision(mTQueue, sampleRays, objects);
|
---|
[1323] | 714 | }
|
---|
[1784] | 715 |
|
---|
[1624] | 716 | // begin subdivision
|
---|
[1667] | 717 | RunConstruction(mRepairQueue, sampleRays, objects, forcedViewSpace);
|
---|
[1308] | 718 |
|
---|
[1418] | 719 | cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[1370] | 720 |
|
---|
[1548] | 721 | mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
|
---|
| 722 | mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
|
---|
| 723 |
|
---|
[1313] | 724 | mHierarchyStats.Stop();
|
---|
[1259] | 725 | mVspTree->mVspStats.Stop();
|
---|
[1649] | 726 |
|
---|
| 727 | FinishObjectSpaceSubdivision(objects, !mUseMultiLevelConstruction);
|
---|
[1237] | 728 | }
|
---|
| 729 |
|
---|
| 730 |
|
---|
[1779] | 731 | void HierarchyManager::PrepareViewSpaceSubdivision(SplitQueue &tQueue,
|
---|
| 732 | const VssRayContainer &sampleRays,
|
---|
| 733 | const ObjectContainer &objects)
|
---|
[1311] | 734 | {
|
---|
[1632] | 735 | cout << "\npreparing view space hierarchy construction ... " << endl;
|
---|
[1625] | 736 |
|
---|
[1580] | 737 | // hack: reset global cost misses
|
---|
[1548] | 738 | mHierarchyStats.mGlobalCostMisses = 0;
|
---|
[1625] | 739 |
|
---|
[1311] | 740 | RayInfoContainer *viewSpaceRays = new RayInfoContainer();
|
---|
[1779] | 741 | mVspTree->PrepareConstruction(tQueue, sampleRays, *viewSpaceRays);
|
---|
[1311] | 742 |
|
---|
[1661] | 743 | /////////
|
---|
| 744 | //-- new stats
|
---|
| 745 |
|
---|
[1624] | 746 | mHierarchyStats.mTotalCost = mVspTree->mTotalCost;
|
---|
| 747 | cout << "\nreseting cost for vsp, new total cost: " << mHierarchyStats.mTotalCost << endl;
|
---|
[1311] | 748 | }
|
---|
| 749 |
|
---|
| 750 |
|
---|
[1640] | 751 | float HierarchyManager::GetObjectSpaceMemUsage() const
|
---|
| 752 | {
|
---|
| 753 | if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
|
---|
| 754 | {
|
---|
| 755 | // TODO;
|
---|
| 756 | }
|
---|
| 757 | else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
|
---|
| 758 | {
|
---|
| 759 | return mBvHierarchy->GetMemUsage();
|
---|
| 760 | }
|
---|
| 761 |
|
---|
| 762 | return -1;
|
---|
| 763 | }
|
---|
| 764 |
|
---|
[1844] | 765 |
|
---|
[1640] | 766 | void HierarchyManager::InitialiseObjectSpaceSubdivision(const ObjectContainer &objects)
|
---|
| 767 | {
|
---|
| 768 | if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
|
---|
| 769 | {
|
---|
| 770 | // TODO;
|
---|
| 771 | }
|
---|
| 772 | else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
|
---|
| 773 | {
|
---|
| 774 | mBvHierarchy->Initialise(objects);
|
---|
| 775 | }
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 |
|
---|
[1779] | 779 | void HierarchyManager::PrepareObjectSpaceSubdivision(SplitQueue &tQueue,
|
---|
| 780 | const VssRayContainer &sampleRays,
|
---|
| 781 | const ObjectContainer &objects)
|
---|
[1308] | 782 | {
|
---|
[1625] | 783 | // hack: reset global cost misses
|
---|
| 784 | mHierarchyStats.mGlobalCostMisses = 0;
|
---|
[1522] | 785 |
|
---|
[1308] | 786 | if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
|
---|
| 787 | {
|
---|
[1779] | 788 | return PrepareOspTree(tQueue, sampleRays, objects);
|
---|
[1308] | 789 | }
|
---|
| 790 | else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
|
---|
| 791 | {
|
---|
[1779] | 792 | return PrepareBvHierarchy(tQueue, sampleRays, objects);
|
---|
[1308] | 793 | }
|
---|
| 794 | }
|
---|
[1294] | 795 |
|
---|
[1308] | 796 |
|
---|
[1779] | 797 | void HierarchyManager::PrepareBvHierarchy(SplitQueue &tQueue,
|
---|
| 798 | const VssRayContainer &sampleRays,
|
---|
| 799 | const ObjectContainer &objects)
|
---|
[1294] | 800 | {
|
---|
[1421] | 801 | const long startTime = GetTime();
|
---|
| 802 |
|
---|
| 803 | cout << "preparing bv hierarchy construction ... " << endl;
|
---|
[1548] | 804 |
|
---|
[1294] | 805 | // compute first candidate
|
---|
[1779] | 806 | mBvHierarchy->PrepareConstruction(tQueue, sampleRays, objects);
|
---|
[1294] | 807 |
|
---|
[1624] | 808 | mHierarchyStats.mTotalCost = mBvHierarchy->mTotalCost;
|
---|
| 809 | Debug << "\nreseting cost, new total cost: " << mHierarchyStats.mTotalCost << endl;
|
---|
[1294] | 810 |
|
---|
[1548] | 811 | cout << "finished bv hierarchy preparation in "
|
---|
| 812 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[1294] | 813 | }
|
---|
| 814 |
|
---|
| 815 |
|
---|
[1779] | 816 | void HierarchyManager::PrepareOspTree(SplitQueue &tQueue,
|
---|
| 817 | const VssRayContainer &sampleRays,
|
---|
| 818 | const ObjectContainer &objects)
|
---|
[1294] | 819 | {
|
---|
[1370] | 820 | cout << "starting osp tree construction ... " << endl;
|
---|
| 821 |
|
---|
[1294] | 822 | RayInfoContainer *objectSpaceRays = new RayInfoContainer();
|
---|
| 823 |
|
---|
| 824 | // start with one big kd cell - all objects can be seen from everywhere
|
---|
| 825 | // note: only true for view space = object space
|
---|
| 826 |
|
---|
| 827 | // compute first candidate
|
---|
[1779] | 828 | mOspTree->PrepareConstruction(tQueue, sampleRays, objects, *objectSpaceRays);
|
---|
[1294] | 829 |
|
---|
[1624] | 830 | mHierarchyStats.mTotalCost = mOspTree->mTotalCost;
|
---|
[1625] | 831 | Debug << "\nreseting cost for osp, new total cost: " << mHierarchyStats.mTotalCost << endl;
|
---|
[1294] | 832 | }
|
---|
| 833 |
|
---|
| 834 |
|
---|
[1912] | 835 | float HierarchyManager::EvalCorrectedPvs(const float childPvs,
|
---|
| 836 | const float totalPvs,
|
---|
[2227] | 837 | const float avgRaysPerObjects) const
|
---|
[1911] | 838 | {
|
---|
[2228] | 839 | // don't correct pvss
|
---|
| 840 | if (mMaxAvgRaysPerObject <= mMinAvgRaysPerObject)
|
---|
| 841 | {
|
---|
| 842 | return childPvs;
|
---|
| 843 | }
|
---|
| 844 |
|
---|
[1912] | 845 | // assume pvs sampled sufficiently => take child pvs
|
---|
[2227] | 846 | if (avgRaysPerObjects > mMaxAvgRaysPerObject)
|
---|
[1913] | 847 | {
|
---|
[1912] | 848 | return childPvs;
|
---|
[1913] | 849 | }
|
---|
[2228] | 850 |
|
---|
| 851 | // assume pvs not sampled sufficiently => pvs equal to parent pvs
|
---|
| 852 | // we should not subdivide further from this point
|
---|
| 853 | if (avgRaysPerObjects <= mMinAvgRaysPerObject)
|
---|
[1913] | 854 | {
|
---|
[2228] | 855 | cout << "t ";// << avgRaysPerObjects << " ";
|
---|
[1912] | 856 | return totalPvs;
|
---|
[1913] | 857 | }
|
---|
[1911] | 858 |
|
---|
[2228] | 859 | ///////////
|
---|
| 860 | //-- blend pvss
|
---|
| 861 |
|
---|
[2227] | 862 | const float alpha = (mMaxAvgRaysPerObject - avgRaysPerObjects) /
|
---|
| 863 | (mMaxAvgRaysPerObject - mMinAvgRaysPerObject);
|
---|
[1911] | 864 |
|
---|
[2227] | 865 | /// rays per object == max threshold => alpha == 0 => newPvs is childPvs
|
---|
| 866 | /// rays per object == min threshold => alpha == 1 => newPvs is totalPvs
|
---|
| 867 |
|
---|
| 868 | const float beta = alpha * (totalPvs - childPvs);
|
---|
[1915] | 869 | #if 1
|
---|
| 870 | const float newPvs = childPvs + beta;
|
---|
| 871 | #else
|
---|
| 872 | const float newPvs = alpha * childPvs + (1.0f - alpha) * totalPvs;
|
---|
| 873 | #endif
|
---|
[2228] | 874 | cout << "b ";// << avgRaysPerObjects << " ";
|
---|
[1916] | 875 | //cout << "alpha " << alpha << " beta: " << beta << " child: " << childPvs << " parent: " << totalPvs << endl;
|
---|
[1913] | 876 |
|
---|
| 877 | if ((newPvs < childPvs - Limits::Small) || (newPvs > totalPvs + Limits::Small))
|
---|
[2227] | 878 | cout << "Error! " << newPvs << " smaller than child pvs or larger than parent pvs" << endl;
|
---|
| 879 |
|
---|
[1913] | 880 | return newPvs;
|
---|
[1911] | 881 | }
|
---|
| 882 |
|
---|
| 883 |
|
---|
[1624] | 884 | bool HierarchyManager::ApplySubdivisionCandidate(SubdivisionCandidate *sc,
|
---|
[1625] | 885 | SplitQueue &splitQueue,
|
---|
[2224] | 886 | //const bool repairQueue,
|
---|
| 887 | SubdivisionCandidateContainer &dirtyList)
|
---|
[1237] | 888 | {
|
---|
[1624] | 889 | const bool terminationCriteriaMet = GlobalTerminationCriteriaMet(sc);
|
---|
[2224] | 890 | const bool success = sc->Apply(splitQueue, terminationCriteriaMet, dirtyList);
|
---|
[1444] | 891 |
|
---|
[1744] | 892 | if (sc->IsDirty())
|
---|
[1745] | 893 | cerr << "Error: Should never come here!" << endl;
|
---|
[1744] | 894 |
|
---|
[1610] | 895 | if (!success) // split was not taken
|
---|
| 896 | {
|
---|
[1763] | 897 | cout << "x";
|
---|
[1610] | 898 | return false;
|
---|
| 899 | }
|
---|
| 900 |
|
---|
[2198] | 901 |
|
---|
[1610] | 902 | ///////////////
|
---|
[1624] | 903 | //-- split was successful => update stats and queue
|
---|
[1610] | 904 |
|
---|
| 905 | // cost ratio of cost decrease / totalCost
|
---|
[1624] | 906 | const float costRatio = sc->GetRenderCostDecrease() / mHierarchyStats.mTotalCost;
|
---|
[1744] | 907 | //cout << "ratio: " << costRatio << " min ratio: " << mTermMinGlobalCostRatio << endl;
|
---|
[1610] | 908 |
|
---|
| 909 | if (costRatio < mTermMinGlobalCostRatio)
|
---|
| 910 | {
|
---|
| 911 | ++ mHierarchyStats.mGlobalCostMisses;
|
---|
| 912 | }
|
---|
| 913 |
|
---|
[1640] | 914 | /////////////
|
---|
[1580] | 915 | // update stats
|
---|
[1640] | 916 |
|
---|
[1624] | 917 | mHierarchyStats.mNodes += 2;
|
---|
[1640] | 918 | mHierarchyStats.mTotalCost -= sc->GetRenderCostDecrease();
|
---|
| 919 |
|
---|
| 920 | const int pvsEntriesIncr = sc->GetPvsEntriesIncr();
|
---|
| 921 | mHierarchyStats.mPvsEntries += pvsEntriesIncr;
|
---|
[2198] | 922 | //cout << "pvs entries: " << pvsEntriesIncr << " " << mHierarchyStats.mPvsEntries << endl;
|
---|
[1640] | 923 |
|
---|
| 924 | // memory size in byte
|
---|
[1895] | 925 | float mem = (float)ObjectPvs::GetEntrySizeByte() * pvsEntriesIncr;
|
---|
| 926 |
|
---|
[2227] | 927 | #if USE_AVGRAYCONTRI
|
---|
[1895] | 928 | // high avg ray contri, the result is influenced by undersampling
|
---|
| 929 | // => decrease priority
|
---|
[2227] | 930 |
|
---|
| 931 | if (sc->GetAvgRayContribution() > mMaxAvgRayContri)
|
---|
[1895] | 932 | {
|
---|
| 933 | const float factor = 1.0f + sc->GetAvgRayContribution() - mMaxAvgRayContri;
|
---|
[1899] | 934 | cout << "h " << factor << endl;
|
---|
[1895] | 935 |
|
---|
| 936 | mem *= factor;
|
---|
| 937 | }
|
---|
[2227] | 938 | #endif
|
---|
| 939 |
|
---|
[1895] | 940 | mHierarchyStats.mMemory += mem;
|
---|
[1624] | 941 | mHierarchyStats.mRenderCostDecrease = sc->GetRenderCostDecrease();
|
---|
[1744] | 942 |
|
---|
| 943 | mPriority = sc->GetPriority();
|
---|
[1624] | 944 |
|
---|
[2228] | 945 | cout << sc->Type() << " ";
|
---|
| 946 |
|
---|
[1895] | 947 | //////////
|
---|
| 948 | // show current memory
|
---|
| 949 |
|
---|
[1654] | 950 | static float memoryCount = 0;
|
---|
| 951 |
|
---|
| 952 | if (mHierarchyStats.mMemory > memoryCount)
|
---|
| 953 | {
|
---|
| 954 | memoryCount += 100000;
|
---|
[1684] | 955 | cout << "\nstorage cost: " << mHierarchyStats.mMemory / float(1024 * 1024)
|
---|
| 956 | << " MB, steps: " << mHierarchyStats.Leaves() << endl;
|
---|
[1654] | 957 | }
|
---|
| 958 |
|
---|
[1640] | 959 | // output stats
|
---|
[1624] | 960 | EvalSubdivisionStats();
|
---|
| 961 |
|
---|
[1416] | 962 | return true;
|
---|
[1237] | 963 | }
|
---|
| 964 |
|
---|
| 965 |
|
---|
[1370] | 966 | int HierarchyManager::GetObjectSpaceSubdivisionDepth() const
|
---|
| 967 | {
|
---|
| 968 | int maxDepth = 0;
|
---|
| 969 |
|
---|
| 970 | if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
|
---|
| 971 | {
|
---|
| 972 | maxDepth = mOspTree->mOspStats.maxDepth;
|
---|
| 973 | }
|
---|
| 974 | else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
|
---|
| 975 | {
|
---|
| 976 | maxDepth = mBvHierarchy->mBvhStats.maxDepth;
|
---|
| 977 | }
|
---|
| 978 |
|
---|
| 979 | return maxDepth;
|
---|
| 980 | }
|
---|
| 981 |
|
---|
| 982 |
|
---|
[1640] | 983 | int HierarchyManager::GetObjectSpaceSubdivisionLeaves() const
|
---|
| 984 | {
|
---|
| 985 | int maxLeaves= 0;
|
---|
| 986 |
|
---|
| 987 | if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
|
---|
| 988 | {
|
---|
| 989 | maxLeaves = mOspTree->mOspStats.Leaves();
|
---|
| 990 | }
|
---|
| 991 | else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
|
---|
| 992 | {
|
---|
| 993 | maxLeaves = mBvHierarchy->mBvhStats.Leaves();
|
---|
| 994 | }
|
---|
| 995 |
|
---|
| 996 | return maxLeaves;
|
---|
| 997 | }
|
---|
| 998 |
|
---|
| 999 |
|
---|
[1663] | 1000 | int HierarchyManager::GetObjectSpaceSubdivisionNodes() const
|
---|
| 1001 | {
|
---|
| 1002 | int maxLeaves = 0;
|
---|
| 1003 |
|
---|
| 1004 | if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
|
---|
| 1005 | {
|
---|
| 1006 | maxLeaves = mOspTree->mOspStats.nodes;
|
---|
| 1007 | }
|
---|
| 1008 | else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
|
---|
| 1009 | {
|
---|
| 1010 | maxLeaves = mBvHierarchy->mBvhStats.nodes;
|
---|
| 1011 | }
|
---|
| 1012 |
|
---|
| 1013 | return maxLeaves;
|
---|
| 1014 | }
|
---|
| 1015 |
|
---|
[1308] | 1016 | bool HierarchyManager::StartObjectSpaceSubdivision() const
|
---|
[1237] | 1017 | {
|
---|
[1370] | 1018 | // view space construction already started
|
---|
| 1019 | if (ObjectSpaceSubdivisionConstructed())
|
---|
| 1020 | return false;
|
---|
| 1021 |
|
---|
| 1022 | // start immediately with object space subdivision?
|
---|
| 1023 | if (mStartWithObjectSpace)
|
---|
| 1024 | return true;
|
---|
| 1025 |
|
---|
| 1026 | // is the queue empty again?
|
---|
| 1027 | if (ViewSpaceSubdivisionConstructed() && mTQueue.Empty())
|
---|
| 1028 | return true;
|
---|
| 1029 |
|
---|
| 1030 | // has the depth for subdivision been reached?
|
---|
| 1031 | return
|
---|
| 1032 | ((mConstructionType == INTERLEAVED) &&
|
---|
[1640] | 1033 | (mMinStepsOfSameType <= mVspTree->mVspStats.nodes));
|
---|
[1308] | 1034 | }
|
---|
| 1035 |
|
---|
| 1036 |
|
---|
[1329] | 1037 | bool HierarchyManager::StartViewSpaceSubdivision() const
|
---|
| 1038 | {
|
---|
[1370] | 1039 | // view space construction already started
|
---|
| 1040 | if (ViewSpaceSubdivisionConstructed())
|
---|
| 1041 | return false;
|
---|
| 1042 |
|
---|
| 1043 | // start immediately with view space subdivision?
|
---|
| 1044 | if (!mStartWithObjectSpace)
|
---|
| 1045 | return true;
|
---|
| 1046 |
|
---|
| 1047 | // is the queue empty again?
|
---|
| 1048 | if (ObjectSpaceSubdivisionConstructed() && mTQueue.Empty())
|
---|
| 1049 | return true;
|
---|
| 1050 |
|
---|
| 1051 | // has the depth for subdivision been reached?
|
---|
| 1052 | return
|
---|
| 1053 | ((mConstructionType == INTERLEAVED) &&
|
---|
[1640] | 1054 | (mMinStepsOfSameType <= GetObjectSpaceSubdivisionLeaves()));
|
---|
[1329] | 1055 | }
|
---|
| 1056 |
|
---|
| 1057 |
|
---|
[1449] | 1058 | void HierarchyManager::RunConstruction(const bool repairQueue,
|
---|
| 1059 | const VssRayContainer &sampleRays,
|
---|
[1311] | 1060 | const ObjectContainer &objects,
|
---|
[1640] | 1061 | AxisAlignedBox3 *forcedViewSpace)
|
---|
[1308] | 1062 | {
|
---|
[2224] | 1063 | SubdivisionCandidate::NewMail();
|
---|
| 1064 |
|
---|
[1313] | 1065 | while (!FinishedConstruction())
|
---|
| 1066 | {
|
---|
[1625] | 1067 | SubdivisionCandidate *sc = NextSubdivisionCandidate(mTQueue);
|
---|
[1632] | 1068 |
|
---|
[1415] | 1069 | ///////////////////
|
---|
[1237] | 1070 | //-- subdivide leaf node
|
---|
[1415] | 1071 |
|
---|
[2224] | 1072 | SubdivisionCandidateContainer dirtyList;
|
---|
| 1073 |
|
---|
| 1074 | ApplySubdivisionCandidate(sc, mTQueue, dirtyList);
|
---|
[1580] | 1075 |
|
---|
[2224] | 1076 | if (repairQueue)
|
---|
| 1077 | {
|
---|
| 1078 | // reevaluate candidates affected by the split for view space splits,
|
---|
| 1079 | // this would be object space splits and other way round
|
---|
| 1080 | RepairQueue(dirtyList, mTQueue, mRecomputeSplitPlaneOnRepair);
|
---|
| 1081 | }
|
---|
| 1082 |
|
---|
[1313] | 1083 | // we use objects for evaluating vsp tree construction until
|
---|
[1415] | 1084 | // a certain depth once a certain depth existiert ...
|
---|
[1313] | 1085 | if (StartObjectSpaceSubdivision())
|
---|
| 1086 | {
|
---|
[1323] | 1087 | mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
|
---|
[1314] | 1088 |
|
---|
[1640] | 1089 | cout << "\nstarting object space subdivision after "
|
---|
[1723] | 1090 | << mVspTree->mVspStats.nodes << " (" << mMinStepsOfSameType << ") steps, mem="
|
---|
| 1091 | << mHierarchyStats.mMemory / float(1024 * 1024) << " MB" << endl;
|
---|
[1329] | 1092 |
|
---|
[1779] | 1093 | PrepareObjectSpaceSubdivision(mTQueue, sampleRays, objects);
|
---|
[1640] | 1094 |
|
---|
[1313] | 1095 | cout << "reseting queue ... ";
|
---|
[1640] | 1096 | ResetQueue(mTQueue, mRecomputeSplitPlaneOnRepair);
|
---|
[1313] | 1097 | cout << "finished" << endl;
|
---|
| 1098 | }
|
---|
| 1099 |
|
---|
[1329] | 1100 | if (StartViewSpaceSubdivision())
|
---|
| 1101 | {
|
---|
| 1102 | mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
|
---|
| 1103 |
|
---|
[1723] | 1104 | cout << "\nstarting view space subdivision at "
|
---|
[1640] | 1105 | << GetObjectSpaceSubdivisionLeaves() << " ("
|
---|
[1723] | 1106 | << mMinStepsOfSameType << ") , mem="
|
---|
| 1107 | << mHierarchyStats.mMemory / float(1024 * 1024) << " MB" << endl;
|
---|
[1329] | 1108 |
|
---|
[1779] | 1109 | PrepareViewSpaceSubdivision(mTQueue, sampleRays, objects);
|
---|
[1329] | 1110 |
|
---|
| 1111 | cout << "reseting queue ... ";
|
---|
[1640] | 1112 | ResetQueue(mTQueue, mRecomputeSplitPlaneOnRepair);
|
---|
[1329] | 1113 | cout << "finished" << endl;
|
---|
| 1114 | }
|
---|
| 1115 |
|
---|
[1624] | 1116 | DEL_PTR(sc);
|
---|
[1237] | 1117 | }
|
---|
| 1118 | }
|
---|
| 1119 |
|
---|
| 1120 |
|
---|
[1449] | 1121 | void HierarchyManager::RunConstruction(const bool repairQueue)
|
---|
| 1122 | {
|
---|
[1580] | 1123 | // main loop
|
---|
[1449] | 1124 | while (!FinishedConstruction())
|
---|
| 1125 | {
|
---|
[1625] | 1126 | SubdivisionCandidate *sc = NextSubdivisionCandidate(mTQueue);
|
---|
[1649] | 1127 |
|
---|
[1624] | 1128 | ////////
|
---|
| 1129 | //-- subdivide leaf node of either type
|
---|
[1904] | 1130 |
|
---|
[2224] | 1131 | SubdivisionCandidateContainer dirtyList;
|
---|
| 1132 | ApplySubdivisionCandidate(sc, mTQueue, dirtyList);
|
---|
[1642] | 1133 |
|
---|
[2224] | 1134 | if (repairQueue)
|
---|
| 1135 | {
|
---|
| 1136 | RepairQueue(dirtyList, mTQueue, mRecomputeSplitPlaneOnRepair);
|
---|
| 1137 | }
|
---|
| 1138 |
|
---|
[1624] | 1139 | DEL_PTR(sc);
|
---|
[1449] | 1140 | }
|
---|
| 1141 | }
|
---|
| 1142 |
|
---|
| 1143 |
|
---|
[1625] | 1144 | int HierarchyManager::RunConstruction(SplitQueue &splitQueue,
|
---|
[1633] | 1145 | SubdivisionCandidateContainer &dirtyCandidates,
|
---|
[1667] | 1146 | SubdivisionCandidate *oldCandidate,
|
---|
[1676] | 1147 | const int minSteps,
|
---|
| 1148 | const int maxSteps)
|
---|
[1624] | 1149 | {
|
---|
[1676] | 1150 | if (minSteps >= maxSteps)
|
---|
| 1151 | cout << "error!! " << minSteps << " equal or larger maxSteps" << endl;
|
---|
| 1152 |
|
---|
[1625] | 1153 | int steps = 0;
|
---|
[1633] | 1154 | SubdivisionCandidate::NewMail();
|
---|
[1625] | 1155 |
|
---|
[1624] | 1156 | // main loop
|
---|
[1634] | 1157 | while (!splitQueue.Empty())
|
---|
[1624] | 1158 | {
|
---|
[1701] | 1159 | const float priority = splitQueue.Top()->GetPriority();
|
---|
[1686] | 1160 | const float threshold = oldCandidate ? oldCandidate->GetPriority() : 1e20f;
|
---|
[1679] | 1161 |
|
---|
[1701] | 1162 | // minimum slope reached
|
---|
| 1163 | if ((steps >= maxSteps) || ((priority < threshold) && !(steps < minSteps)))
|
---|
| 1164 | {
|
---|
[1745] | 1165 | cout << "\nbreaking on " << priority << " smaller than " << threshold << endl;
|
---|
[1701] | 1166 | break;
|
---|
| 1167 | }
|
---|
| 1168 |
|
---|
[1624] | 1169 | ////////
|
---|
| 1170 | //-- subdivide leaf node of either type
|
---|
| 1171 |
|
---|
[1727] | 1172 | SubdivisionCandidate *sc = NextSubdivisionCandidate(splitQueue);
|
---|
[2227] | 1173 |
|
---|
[2224] | 1174 | const bool success = ApplySubdivisionCandidate(sc, splitQueue, dirtyCandidates);
|
---|
[1625] | 1175 |
|
---|
[1632] | 1176 | if (success)
|
---|
| 1177 | {
|
---|
[2224] | 1178 | //sc->CollectDirtyCandidates(dirtyCandidates, true);
|
---|
[2228] | 1179 | //if (steps % 10 == 0) cout << sc->Type() << " ";
|
---|
[2227] | 1180 |
|
---|
[1633] | 1181 | ++ steps;
|
---|
[1632] | 1182 | }
|
---|
[1696] | 1183 |
|
---|
| 1184 | DEL_PTR(sc);
|
---|
[1624] | 1185 | }
|
---|
[1625] | 1186 |
|
---|
| 1187 | return steps;
|
---|
[1624] | 1188 | }
|
---|
| 1189 |
|
---|
| 1190 |
|
---|
[1779] | 1191 | void HierarchyManager::ResetObjectSpaceSubdivision(SplitQueue &tQueue,
|
---|
| 1192 | const VssRayContainer &sampleRays,
|
---|
| 1193 | const ObjectContainer &objects)
|
---|
[1580] | 1194 | {
|
---|
| 1195 | // object space partition constructed => reconstruct
|
---|
[1557] | 1196 | switch (mObjectSpaceSubdivisionType)
|
---|
| 1197 | {
|
---|
| 1198 | case BV_BASED_OBJ_SUBDIV:
|
---|
[1580] | 1199 | {
|
---|
| 1200 | cout << "\nreseting bv hierarchy" << endl;
|
---|
| 1201 | Debug << "old bv hierarchy:\n " << mBvHierarchy->mBvhStats << endl;
|
---|
[1663] | 1202 |
|
---|
| 1203 | // rather use this: remove previous nodes and add the two new ones
|
---|
| 1204 | //mHierarchyStats.mNodes -= mBvHierarchy->mBvhStats.nodes + 1;
|
---|
| 1205 | mHierarchyStats.mNodes = mVspTree->mVspStats.nodes;
|
---|
[1649] | 1206 |
|
---|
[1663] | 1207 | // create root
|
---|
[1642] | 1208 | mBvHierarchy->Initialise(objects);
|
---|
[1649] | 1209 |
|
---|
[1779] | 1210 | mBvHierarchy->Reset(tQueue, sampleRays, objects);
|
---|
[1649] | 1211 |
|
---|
[1625] | 1212 | mHierarchyStats.mTotalCost = mBvHierarchy->mTotalCost;
|
---|
[1663] | 1213 |
|
---|
[1662] | 1214 | //mHierarchyStats.mPvsEntries -= mBvHierarchy->mPvsEntries + 1;
|
---|
[1649] | 1215 | mHierarchyStats.mPvsEntries = mBvHierarchy->CountViewCells(objects);
|
---|
[1663] | 1216 |
|
---|
[1667] | 1217 | mHierarchyStats.mMemory =
|
---|
| 1218 | (float)mHierarchyStats.mPvsEntries * ObjectPvs::GetEntrySizeByte();
|
---|
[1642] | 1219 |
|
---|
[1662] | 1220 | mHierarchyStats.mRenderCostDecrease = 0;
|
---|
| 1221 |
|
---|
[1625] | 1222 | // evaluate stats before first subdivision
|
---|
| 1223 | EvalSubdivisionStats();
|
---|
[1649] | 1224 | cout << "finished bv hierarchy preparation" << endl;
|
---|
[1580] | 1225 | }
|
---|
[1548] | 1226 | break;
|
---|
[1557] | 1227 |
|
---|
[1548] | 1228 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 1229 | // TODO
|
---|
| 1230 | default:
|
---|
| 1231 | break;
|
---|
| 1232 | }
|
---|
[1449] | 1233 | }
|
---|
| 1234 |
|
---|
| 1235 |
|
---|
[1779] | 1236 | void HierarchyManager::ResetViewSpaceSubdivision(SplitQueue &tQueue,
|
---|
| 1237 | const VssRayContainer &sampleRays,
|
---|
| 1238 | const ObjectContainer &objects,
|
---|
| 1239 | AxisAlignedBox3 *forcedViewSpace)
|
---|
[1557] | 1240 | {
|
---|
[1649] | 1241 | ViewCellsManager *vm = mVspTree->mViewCellsManager;
|
---|
[1557] | 1242 |
|
---|
[1624] | 1243 | // HACK: rather not destroy vsp tree
|
---|
[1580] | 1244 | DEL_PTR(mVspTree);
|
---|
| 1245 | mVspTree = new VspTree();
|
---|
[1649] | 1246 |
|
---|
[1580] | 1247 | mVspTree->mHierarchyManager = this;
|
---|
[1649] | 1248 | mVspTree->mViewCellsManager = vm;
|
---|
[1580] | 1249 |
|
---|
[1642] | 1250 | mVspTree->Initialise(sampleRays, forcedViewSpace);
|
---|
[1580] | 1251 |
|
---|
[1779] | 1252 | //////////
|
---|
[1662] | 1253 | //-- reset stats
|
---|
[1779] | 1254 | mHierarchyStats.mNodes = GetObjectSpaceSubdivisionNodes();
|
---|
| 1255 | //-mVspTree->mVspStats.nodes + 1;
|
---|
[1662] | 1256 |
|
---|
[1779] | 1257 | PrepareViewSpaceSubdivision(mTQueue, sampleRays, objects);
|
---|
[1662] | 1258 |
|
---|
| 1259 | mHierarchyStats.mPvsEntries = mVspTree->mPvsEntries;
|
---|
[1661] | 1260 | mHierarchyStats.mRenderCostDecrease = 0;
|
---|
| 1261 |
|
---|
[1779] | 1262 | mHierarchyStats.mMemory =
|
---|
| 1263 | (float)mHierarchyStats.mPvsEntries * ObjectPvs::GetEntrySizeByte();
|
---|
[1662] | 1264 |
|
---|
[1640] | 1265 | // evaluate new stats before first subdivsiion
|
---|
[1624] | 1266 | EvalSubdivisionStats();
|
---|
[1557] | 1267 | }
|
---|
| 1268 |
|
---|
| 1269 |
|
---|
[1449] | 1270 | void HierarchyManager::ConstructMultiLevel(const VssRayContainer &sampleRays,
|
---|
| 1271 | const ObjectContainer &objects,
|
---|
| 1272 | AxisAlignedBox3 *forcedViewSpace)
|
---|
| 1273 | {
|
---|
| 1274 | mHierarchyStats.Reset();
|
---|
| 1275 | mHierarchyStats.Start();
|
---|
[1624] | 1276 | mHierarchyStats.mNodes = 2;
|
---|
[1449] | 1277 |
|
---|
[1624] | 1278 | mHierarchyStats.mTotalCost = (float)objects.size();
|
---|
| 1279 | Debug << "setting total cost to " << mHierarchyStats.mTotalCost << endl;
|
---|
[1449] | 1280 |
|
---|
| 1281 | const long startTime = GetTime();
|
---|
| 1282 | cout << "Constructing view space / object space tree ... \n";
|
---|
| 1283 |
|
---|
[1640] | 1284 | // initialise view / object space
|
---|
| 1285 | mVspTree->Initialise(sampleRays, forcedViewSpace);
|
---|
| 1286 | InitialiseObjectSpaceSubdivision(objects);
|
---|
[1449] | 1287 |
|
---|
[1580] | 1288 | // use sah for evaluating osp tree construction
|
---|
| 1289 | // in the first iteration of the subdivision
|
---|
[1642] | 1290 |
|
---|
[1449] | 1291 | mSavedViewSpaceSubdivisionType = mViewSpaceSubdivisionType;
|
---|
| 1292 | mViewSpaceSubdivisionType = NO_VIEWSPACE_SUBDIV;
|
---|
[1640] | 1293 |
|
---|
[1779] | 1294 | PrepareObjectSpaceSubdivision(mTQueue, sampleRays, objects);
|
---|
[1640] | 1295 |
|
---|
[1649] | 1296 | //////////////////////////
|
---|
[1642] | 1297 |
|
---|
| 1298 |
|
---|
[1640] | 1299 | const int limit = mNumMultiLevels;
|
---|
| 1300 | int i = 0;
|
---|
| 1301 |
|
---|
| 1302 | // This method subdivides view space / object space
|
---|
| 1303 | // in order to converge to some optimal cost for this partition
|
---|
| 1304 | // start with object space partiton
|
---|
| 1305 | // then optimizate view space partition for the current osp
|
---|
| 1306 | // and vice versa until iteration depth is reached.
|
---|
| 1307 | while (1)
|
---|
| 1308 | {
|
---|
| 1309 | char subdivisionStatsLog[100];
|
---|
| 1310 | sprintf(subdivisionStatsLog, "tests/i3d/subdivision-%04d.log", i);
|
---|
| 1311 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
| 1312 |
|
---|
| 1313 | // subdivide object space first
|
---|
[1779] | 1314 | ResetObjectSpaceSubdivision(mTQueue, sampleRays, objects);
|
---|
[1640] | 1315 |
|
---|
| 1316 | // process object space candidates
|
---|
| 1317 | RunConstruction(false);
|
---|
| 1318 |
|
---|
| 1319 | // object space subdivision constructed
|
---|
| 1320 | mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
|
---|
| 1321 |
|
---|
| 1322 | cout << "iteration " << i << " of " << limit << " finished" << endl;
|
---|
| 1323 | mSubdivisionStats.close();
|
---|
| 1324 |
|
---|
| 1325 | if ((i ++) >= limit)
|
---|
| 1326 | break;
|
---|
| 1327 |
|
---|
| 1328 | sprintf(subdivisionStatsLog, "tests/i3d/subdivision-%04d.log", i);
|
---|
| 1329 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
| 1330 |
|
---|
[1649] | 1331 |
|
---|
[1640] | 1332 | /////////////////
|
---|
| 1333 | // subdivide view space with respect to the objects
|
---|
| 1334 |
|
---|
[1779] | 1335 | ResetViewSpaceSubdivision(mTQueue, sampleRays, objects, forcedViewSpace);
|
---|
| 1336 |
|
---|
[1640] | 1337 | // view space subdivision constructed
|
---|
| 1338 | mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
|
---|
| 1339 |
|
---|
| 1340 | // process view space candidates
|
---|
| 1341 | RunConstruction(false);
|
---|
| 1342 |
|
---|
| 1343 | cout << "iteration " << i << " of " << limit << " finished" << endl;
|
---|
| 1344 | mSubdivisionStats.close();
|
---|
| 1345 |
|
---|
| 1346 | if ((i ++) >= limit)
|
---|
| 1347 | break;
|
---|
| 1348 | }
|
---|
[1635] | 1349 |
|
---|
[1640] | 1350 | cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[1580] | 1351 |
|
---|
[1640] | 1352 | mHierarchyStats.Stop();
|
---|
| 1353 | mVspTree->mVspStats.Stop();
|
---|
| 1354 | FinishObjectSpaceSubdivision(objects);
|
---|
| 1355 | }
|
---|
| 1356 |
|
---|
| 1357 |
|
---|
| 1358 | void HierarchyManager::OptimizeMultiLevel(const VssRayContainer &sampleRays,
|
---|
| 1359 | const ObjectContainer &objects,
|
---|
| 1360 | AxisAlignedBox3 *forcedViewSpace)
|
---|
| 1361 | {
|
---|
[1649] | 1362 | const long startTime = GetTime();
|
---|
| 1363 | const int limit = mNumMultiLevels;
|
---|
| 1364 |
|
---|
| 1365 | // open up new subdivision
|
---|
| 1366 | mSubdivisionStats.close();
|
---|
| 1367 |
|
---|
| 1368 | int steps = 0;
|
---|
| 1369 |
|
---|
| 1370 | int maxViewSpaceLeaves = mVspTree->mVspStats.Leaves();
|
---|
| 1371 | int maxObjectSpaceLeaves;
|
---|
| 1372 |
|
---|
[1642] | 1373 | // set the number of leaves 'evaluated' from the previous methods
|
---|
[1649] | 1374 | // we go for the same numbers, but we try to optimize both subdivisions
|
---|
[1642] | 1375 | switch (mObjectSpaceSubdivisionType)
|
---|
| 1376 | {
|
---|
| 1377 | case BV_BASED_OBJ_SUBDIV:
|
---|
[1649] | 1378 | maxObjectSpaceLeaves = mBvHierarchy->mBvhStats.Leaves();
|
---|
[1642] | 1379 | break;
|
---|
| 1380 | case KD_BASED_OBJ_SUBDIV:
|
---|
[1649] | 1381 | maxObjectSpaceLeaves = mOspTree->mOspStats.Leaves();
|
---|
[1642] | 1382 | default:
|
---|
[1649] | 1383 | maxObjectSpaceLeaves = 0;
|
---|
[1642] | 1384 | break;
|
---|
| 1385 | }
|
---|
[1640] | 1386 |
|
---|
[1624] | 1387 | // This method subdivides view space / object space
|
---|
| 1388 | // in order to converge to some optimal cost for this partition
|
---|
[1557] | 1389 | // start with object space partiton
|
---|
| 1390 | // then optimizate view space partition for the current osp
|
---|
| 1391 | // and vice versa until iteration depth is reached.
|
---|
| 1392 | while (1)
|
---|
[1548] | 1393 | {
|
---|
[1580] | 1394 | char subdivisionStatsLog[100];
|
---|
[1642] | 1395 | sprintf(subdivisionStatsLog, "tests/i3d/subdivision-%04d.log", steps);
|
---|
[1580] | 1396 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
| 1397 |
|
---|
[1624] | 1398 | // subdivide object space first
|
---|
[1779] | 1399 | ResetObjectSpaceSubdivision(mTQueue, sampleRays, objects);
|
---|
[1649] | 1400 |
|
---|
| 1401 | // set the number of leaves 'evaluated' from the previous methods
|
---|
| 1402 | // we go for the same numbers, but we try to optimize both subdivisions
|
---|
| 1403 | mBvHierarchy->mTermMaxLeaves = maxObjectSpaceLeaves;
|
---|
[1624] | 1404 |
|
---|
[1548] | 1405 | // process object space candidates
|
---|
| 1406 | RunConstruction(false);
|
---|
[1449] | 1407 |
|
---|
[1642] | 1408 | cout << "iteration " << steps << " of " << limit << " finished" << endl;
|
---|
[1580] | 1409 | mSubdivisionStats.close();
|
---|
[1635] | 1410 |
|
---|
[1642] | 1411 | if ((++ steps) >= limit)
|
---|
[1557] | 1412 | break;
|
---|
| 1413 |
|
---|
[1642] | 1414 | sprintf(subdivisionStatsLog, "tests/i3d/subdivision-%04d.log", steps);
|
---|
[1580] | 1415 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
| 1416 |
|
---|
[1557] | 1417 | /////////////////
|
---|
[1580] | 1418 | // subdivide view space with respect to the objects
|
---|
| 1419 |
|
---|
[1779] | 1420 | ResetViewSpaceSubdivision(mTQueue, sampleRays, objects, forcedViewSpace);
|
---|
[1649] | 1421 |
|
---|
| 1422 | mVspTree->mMaxViewCells = maxViewSpaceLeaves;
|
---|
[1624] | 1423 |
|
---|
[1548] | 1424 | // process view space candidates
|
---|
| 1425 | RunConstruction(false);
|
---|
[1624] | 1426 |
|
---|
[1642] | 1427 | cout << "iteration " << steps << " of " << limit << " finished" << endl;
|
---|
[1580] | 1428 | mSubdivisionStats.close();
|
---|
| 1429 |
|
---|
[1642] | 1430 | if ((++ steps) >= limit)
|
---|
[1557] | 1431 | break;
|
---|
[1548] | 1432 | }
|
---|
| 1433 |
|
---|
[1449] | 1434 | cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
| 1435 |
|
---|
| 1436 | mHierarchyStats.Stop();
|
---|
| 1437 | mVspTree->mVspStats.Stop();
|
---|
| 1438 | FinishObjectSpaceSubdivision(objects);
|
---|
| 1439 | }
|
---|
| 1440 |
|
---|
| 1441 |
|
---|
[1640] | 1442 |
|
---|
[1237] | 1443 | bool HierarchyManager::FinishedConstruction() const
|
---|
| 1444 | {
|
---|
| 1445 | return mTQueue.Empty();
|
---|
| 1446 | }
|
---|
| 1447 |
|
---|
| 1448 |
|
---|
[1313] | 1449 | bool HierarchyManager::ObjectSpaceSubdivisionConstructed() const
|
---|
[1311] | 1450 | {
|
---|
[1642] | 1451 | /*switch (mObjectSpaceSubdivisionType)
|
---|
[1311] | 1452 | {
|
---|
| 1453 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 1454 | return mOspTree && mOspTree->GetRoot();
|
---|
| 1455 | case BV_BASED_OBJ_SUBDIV:
|
---|
[1344] | 1456 | return mBvHierarchy && mBvHierarchy->GetRoot();
|
---|
[1311] | 1457 | default:
|
---|
[1580] | 1458 | return false;
|
---|
[1642] | 1459 | }*/
|
---|
| 1460 | return mObjectSpaceSubdivisionType != NO_OBJ_SUBDIV;
|
---|
[1311] | 1461 | }
|
---|
| 1462 |
|
---|
| 1463 |
|
---|
[1329] | 1464 | bool HierarchyManager::ViewSpaceSubdivisionConstructed() const
|
---|
| 1465 | {
|
---|
[1635] | 1466 | return mViewSpaceSubdivisionType != NO_VIEWSPACE_SUBDIV;
|
---|
| 1467 | //return mVspTree && mVspTree->GetRoot();
|
---|
[1329] | 1468 | }
|
---|
| 1469 |
|
---|
| 1470 |
|
---|
[1625] | 1471 | void HierarchyManager::CollectDirtyCandidates(const SubdivisionCandidateContainer &chosenCandidates,
|
---|
| 1472 | SubdivisionCandidateContainer &dirtyList)
|
---|
[1237] | 1473 | {
|
---|
[1625] | 1474 | SubdivisionCandidateContainer::const_iterator sit, sit_end = chosenCandidates.end();
|
---|
[1633] | 1475 | SubdivisionCandidate::NewMail();
|
---|
[1625] | 1476 |
|
---|
| 1477 | for (sit = chosenCandidates.begin(); sit != sit_end; ++ sit)
|
---|
| 1478 | {
|
---|
[1633] | 1479 | (*sit)->CollectDirtyCandidates(dirtyList, true);
|
---|
[1625] | 1480 | }
|
---|
| 1481 | }
|
---|
| 1482 |
|
---|
| 1483 |
|
---|
[1736] | 1484 | int HierarchyManager::RepairQueue(const SubdivisionCandidateContainer &dirtyList,
|
---|
| 1485 | SplitQueue &splitQueue,
|
---|
| 1486 | const bool recomputeSplitPlaneOnRepair)
|
---|
[1625] | 1487 | {
|
---|
[1237] | 1488 | // for each update of the view space partition:
|
---|
| 1489 | // the candidates from object space partition which
|
---|
| 1490 | // have been afected by the view space split (the kd split candidates
|
---|
| 1491 | // which saw the view cell which was split) must be reevaluated
|
---|
| 1492 | // (maybe not locally, just reinsert them into the queue)
|
---|
| 1493 | //
|
---|
| 1494 | // vice versa for the view cells
|
---|
| 1495 | // for each update of the object space partition
|
---|
| 1496 | // reevaluate split candidate for view cells which saw the split kd cell
|
---|
| 1497 | //
|
---|
| 1498 | // the priority queue update can be solved by implementing a binary heap
|
---|
| 1499 | // (explicit data structure, binary tree)
|
---|
| 1500 | // *) inserting and removal is efficient
|
---|
| 1501 | // *) search is not efficient => store queue position with each
|
---|
| 1502 | // split candidate
|
---|
| 1503 |
|
---|
[1736] | 1504 | int repaired = 0;
|
---|
| 1505 |
|
---|
[1237] | 1506 | // collect list of "dirty" candidates
|
---|
[1580] | 1507 | const long startTime = GetTime();
|
---|
[1625] | 1508 | if (0) cout << "repairing " << (int)dirtyList.size() << " candidates ... ";
|
---|
[1624] | 1509 |
|
---|
[1727] | 1510 | const float prop = (float)mMaxRepairs / (float)dirtyList.size();
|
---|
| 1511 |
|
---|
[1684] | 1512 | ///////////////////////////
|
---|
[1305] | 1513 | //-- reevaluate the dirty list
|
---|
[1415] | 1514 |
|
---|
[1313] | 1515 | SubdivisionCandidateContainer::const_iterator sit, sit_end = dirtyList.end();
|
---|
[1302] | 1516 |
|
---|
[2227] | 1517 | int i = 0;
|
---|
| 1518 | for (sit = dirtyList.begin(); sit != sit_end; ++ sit, ++ i)
|
---|
[1237] | 1519 | {
|
---|
[1727] | 1520 | // only repair a certain number of candidates
|
---|
| 1521 | if ((mMaxRepairs < (int)dirtyList.size()) && (Random(1.0f) >= prop))
|
---|
| 1522 | continue;
|
---|
| 1523 |
|
---|
[1237] | 1524 | SubdivisionCandidate* sc = *sit;
|
---|
[1305] | 1525 | const float rcd = sc->GetRenderCostDecrease();
|
---|
[1302] | 1526 |
|
---|
[1667] | 1527 | // erase from queue
|
---|
| 1528 | splitQueue.Erase(sc);
|
---|
| 1529 | // reevaluate candidate
|
---|
| 1530 | sc->EvalCandidate(recomputeSplitPlaneOnRepair);
|
---|
| 1531 | // reinsert
|
---|
| 1532 | splitQueue.Push(sc);
|
---|
[1736] | 1533 |
|
---|
| 1534 | ++ repaired;
|
---|
[2227] | 1535 | //if (i % 10 == 0)
|
---|
| 1536 | cout << ".";
|
---|
[1667] | 1537 |
|
---|
[1715] | 1538 | #ifdef GTP_DEBUG
|
---|
[1305] | 1539 | Debug << "candidate " << sc << " reevaluated\n"
|
---|
| 1540 | << "render cost decrease diff " << rcd - sc->GetRenderCostDecrease()
|
---|
[1580] | 1541 | << " old: " << rcd << " new " << sc->GetRenderCostDecrease() << endl;
|
---|
| 1542 | #endif
|
---|
[1237] | 1543 | }
|
---|
[1313] | 1544 |
|
---|
[1625] | 1545 | const long endTime = GetTime();
|
---|
| 1546 | const Real timeDiff = TimeDiff(startTime, endTime);
|
---|
| 1547 |
|
---|
[1624] | 1548 | mHierarchyStats.mRepairTime += timeDiff;
|
---|
[1313] | 1549 |
|
---|
[1736] | 1550 | return repaired;
|
---|
[1237] | 1551 | }
|
---|
| 1552 |
|
---|
[1259] | 1553 |
|
---|
[1640] | 1554 | void HierarchyManager::ResetQueue(SplitQueue &splitQueue, const bool recomputeSplitPlane)
|
---|
[1313] | 1555 | {
|
---|
| 1556 | SubdivisionCandidateContainer mCandidateBuffer;
|
---|
| 1557 |
|
---|
| 1558 | // remove from queue
|
---|
[1640] | 1559 | while (!splitQueue.Empty())
|
---|
[1313] | 1560 | {
|
---|
[1640] | 1561 | SubdivisionCandidate *candidate = NextSubdivisionCandidate(splitQueue);
|
---|
[1744] | 1562 |
|
---|
| 1563 | // reevaluate local split plane and priority
|
---|
[1667] | 1564 | candidate->EvalCandidate(recomputeSplitPlane);
|
---|
[1313] | 1565 | cout << ".";
|
---|
| 1566 | mCandidateBuffer.push_back(candidate);
|
---|
| 1567 | }
|
---|
| 1568 |
|
---|
| 1569 | // put back into queue
|
---|
| 1570 | SubdivisionCandidateContainer::const_iterator sit, sit_end = mCandidateBuffer.end();
|
---|
| 1571 | for (sit = mCandidateBuffer.begin(); sit != sit_end; ++ sit)
|
---|
[1580] | 1572 | {
|
---|
[1640] | 1573 | splitQueue.Push(*sit);
|
---|
[1313] | 1574 | }
|
---|
| 1575 | }
|
---|
| 1576 |
|
---|
| 1577 |
|
---|
[1279] | 1578 | void HierarchyManager::ExportObjectSpaceHierarchy(OUT_STREAM &stream)
|
---|
| 1579 | {
|
---|
| 1580 | // the type of the view cells hierarchy
|
---|
[1308] | 1581 | switch (mObjectSpaceSubdivisionType)
|
---|
[1279] | 1582 | {
|
---|
| 1583 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 1584 | stream << "<ObjectSpaceHierarchy type=\"osp\">" << endl;
|
---|
| 1585 | mOspTree->Export(stream);
|
---|
| 1586 | stream << endl << "</ObjectSpaceHierarchy>" << endl;
|
---|
[1305] | 1587 | break;
|
---|
[1279] | 1588 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 1589 | stream << "<ObjectSpaceHierarchy type=\"bvh\">" << endl;
|
---|
| 1590 | mBvHierarchy->Export(stream);
|
---|
| 1591 | stream << endl << "</ObjectSpaceHierarchy>" << endl;
|
---|
| 1592 | break;
|
---|
| 1593 | }
|
---|
| 1594 | }
|
---|
| 1595 |
|
---|
| 1596 |
|
---|
[1421] | 1597 | void HierarchyManager::PrintHierarchyStatistics(ostream &stream) const
|
---|
[1279] | 1598 | {
|
---|
[1313] | 1599 | stream << mHierarchyStats << endl;
|
---|
| 1600 | stream << "\nview space:" << endl << endl;
|
---|
| 1601 | stream << mVspTree->GetStatistics() << endl;
|
---|
| 1602 | stream << "\nobject space:" << endl << endl;
|
---|
[1370] | 1603 |
|
---|
[1308] | 1604 | switch (mObjectSpaceSubdivisionType)
|
---|
[1279] | 1605 | {
|
---|
| 1606 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 1607 | {
|
---|
[1313] | 1608 | stream << mOspTree->GetStatistics() << endl;
|
---|
[1279] | 1609 | break;
|
---|
| 1610 | }
|
---|
| 1611 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 1612 | {
|
---|
[1313] | 1613 | stream << mBvHierarchy->GetStatistics() << endl;
|
---|
[1279] | 1614 | break;
|
---|
| 1615 | }
|
---|
| 1616 | default:
|
---|
| 1617 | break;
|
---|
| 1618 | }
|
---|
| 1619 | }
|
---|
| 1620 |
|
---|
| 1621 |
|
---|
[1287] | 1622 | void HierarchyManager::ExportObjectSpaceHierarchy(Exporter *exporter,
|
---|
[1416] | 1623 | const ObjectContainer &objects,
|
---|
[1764] | 1624 | AxisAlignedBox3 *bbox,
|
---|
[1920] | 1625 | const float maxRenderCost,
|
---|
[1418] | 1626 | const bool exportBounds) const
|
---|
[1279] | 1627 | {
|
---|
[1308] | 1628 | switch (mObjectSpaceSubdivisionType)
|
---|
[1286] | 1629 | {
|
---|
| 1630 | case KD_BASED_OBJ_SUBDIV:
|
---|
[1279] | 1631 | {
|
---|
[1287] | 1632 | ExportOspTree(exporter, objects);
|
---|
[1286] | 1633 | break;
|
---|
| 1634 | }
|
---|
| 1635 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 1636 | {
|
---|
[1920] | 1637 | exporter->ExportBvHierarchy(*mBvHierarchy, maxRenderCost, bbox, exportBounds);
|
---|
[1286] | 1638 | break;
|
---|
| 1639 | }
|
---|
| 1640 | default:
|
---|
| 1641 | break;
|
---|
| 1642 | }
|
---|
| 1643 | }
|
---|
[1279] | 1644 |
|
---|
[1286] | 1645 |
|
---|
[1287] | 1646 | void HierarchyManager::ExportOspTree(Exporter *exporter,
|
---|
| 1647 | const ObjectContainer &objects) const
|
---|
[1286] | 1648 | {
|
---|
[1287] | 1649 | if (0) exporter->ExportGeometry(objects);
|
---|
[1279] | 1650 |
|
---|
[1287] | 1651 | exporter->SetWireframe();
|
---|
| 1652 | exporter->ExportOspTree(*mOspTree, 0);
|
---|
[1286] | 1653 | }
|
---|
| 1654 |
|
---|
| 1655 |
|
---|
[1743] | 1656 | Intersectable *HierarchyManager::GetIntersectable(Intersectable *obj,
|
---|
| 1657 | const Vector3 &point) const
|
---|
| 1658 | {
|
---|
[1758] | 1659 |
|
---|
| 1660 | if (!obj)
|
---|
| 1661 | return NULL;
|
---|
| 1662 |
|
---|
| 1663 | switch (mObjectSpaceSubdivisionType)
|
---|
[1743] | 1664 | {
|
---|
| 1665 | case HierarchyManager::KD_BASED_OBJ_SUBDIV:
|
---|
[1758] | 1666 | {
|
---|
| 1667 | KdLeaf *leaf = mOspTree->GetLeaf(point, NULL);
|
---|
| 1668 | return mOspTree->GetOrCreateKdIntersectable(leaf);
|
---|
| 1669 | }
|
---|
[1743] | 1670 | case HierarchyManager::BV_BASED_OBJ_SUBDIV:
|
---|
[1758] | 1671 | {
|
---|
| 1672 | BvhLeaf *leaf = mBvHierarchy->GetLeaf(obj);
|
---|
| 1673 | return leaf;
|
---|
| 1674 | }
|
---|
[1743] | 1675 | default:
|
---|
[1758] | 1676 | return obj;
|
---|
[1743] | 1677 | }
|
---|
| 1678 | }
|
---|
| 1679 |
|
---|
[1418] | 1680 | Intersectable *HierarchyManager::GetIntersectable(const VssRay &ray,
|
---|
| 1681 | const bool isTermination) const
|
---|
| 1682 | {
|
---|
[1758] | 1683 | Intersectable *obj = NULL;
|
---|
[1418] | 1684 | Vector3 pt;
|
---|
| 1685 | KdNode *node;
|
---|
| 1686 |
|
---|
| 1687 | ray.GetSampleData(isTermination, pt, &obj, &node);
|
---|
[1758] | 1688 |
|
---|
[1743] | 1689 | if (!obj)
|
---|
[1758] | 1690 | return NULL;
|
---|
[1418] | 1691 |
|
---|
| 1692 | switch (mObjectSpaceSubdivisionType)
|
---|
| 1693 | {
|
---|
| 1694 | case HierarchyManager::KD_BASED_OBJ_SUBDIV:
|
---|
| 1695 | {
|
---|
[1758] | 1696 | KdLeaf *leaf = mOspTree->GetLeaf(pt, node);
|
---|
[1418] | 1697 | return mOspTree->GetOrCreateKdIntersectable(leaf);
|
---|
| 1698 | }
|
---|
| 1699 | case HierarchyManager::BV_BASED_OBJ_SUBDIV:
|
---|
| 1700 | {
|
---|
| 1701 | BvhLeaf *leaf = mBvHierarchy->GetLeaf(obj);
|
---|
[1758] | 1702 | return leaf;
|
---|
[1418] | 1703 | }
|
---|
| 1704 | default:
|
---|
[1758] | 1705 | break;
|
---|
[1418] | 1706 | }
|
---|
[1764] | 1707 |
|
---|
[1743] | 1708 | return obj;
|
---|
[1418] | 1709 | }
|
---|
| 1710 |
|
---|
| 1711 |
|
---|
[1313] | 1712 | void HierarchyStatistics::Print(ostream &app) const
|
---|
| 1713 | {
|
---|
| 1714 | app << "=========== Hierarchy statistics ===============\n";
|
---|
| 1715 |
|
---|
| 1716 | app << setprecision(4);
|
---|
| 1717 |
|
---|
| 1718 | app << "#N_CTIME ( Construction time [s] )\n" << Time() << " \n";
|
---|
| 1719 |
|
---|
[1624] | 1720 | app << "#N_RTIME ( Repair time [s] )\n" << mRepairTime * 1e-3f << " \n";
|
---|
[1313] | 1721 |
|
---|
[1624] | 1722 | app << "#N_NODES ( Number of nodes )\n" << mNodes << "\n";
|
---|
[1313] | 1723 |
|
---|
| 1724 | app << "#N_INTERIORS ( Number of interior nodes )\n" << Interior() << "\n";
|
---|
| 1725 |
|
---|
| 1726 | app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n";
|
---|
| 1727 |
|
---|
[1624] | 1728 | app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << mMaxDepth << endl;
|
---|
[1449] | 1729 |
|
---|
| 1730 | app << "#N_GLOBALCOSTMISSES ( Global cost misses )\n" << mGlobalCostMisses << endl;
|
---|
[1313] | 1731 |
|
---|
| 1732 | app << "========== END OF Hierarchy statistics ==========\n";
|
---|
| 1733 | }
|
---|
| 1734 |
|
---|
| 1735 |
|
---|
[1418] | 1736 | static void RemoveRayRefs(const ObjectContainer &objects)
|
---|
[1370] | 1737 | {
|
---|
[1418] | 1738 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 1739 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 1740 | {
|
---|
[1696] | 1741 | (*oit)->DelRayRefs();
|
---|
[1418] | 1742 | }
|
---|
| 1743 | }
|
---|
| 1744 |
|
---|
| 1745 |
|
---|
[1679] | 1746 | void HierarchyManager::FinishObjectSpaceSubdivision(const ObjectContainer &objects,
|
---|
| 1747 | const bool removeRayRefs) const
|
---|
[1418] | 1748 | {
|
---|
[1370] | 1749 | switch (mObjectSpaceSubdivisionType)
|
---|
| 1750 | {
|
---|
| 1751 | case KD_BASED_OBJ_SUBDIV:
|
---|
| 1752 | {
|
---|
| 1753 | mOspTree->mOspStats.Stop();
|
---|
| 1754 | break;
|
---|
| 1755 | }
|
---|
| 1756 | case BV_BASED_OBJ_SUBDIV:
|
---|
| 1757 | {
|
---|
| 1758 | mBvHierarchy->mBvhStats.Stop();
|
---|
[1649] | 1759 | if (removeRayRefs)
|
---|
| 1760 | RemoveRayRefs(objects);
|
---|
[1370] | 1761 | break;
|
---|
| 1762 | }
|
---|
| 1763 | default:
|
---|
| 1764 | break;
|
---|
| 1765 | }
|
---|
| 1766 | }
|
---|
| 1767 |
|
---|
[1614] | 1768 |
|
---|
| 1769 | void HierarchyManager::ExportBoundingBoxes(OUT_STREAM &stream, const ObjectContainer &objects)
|
---|
| 1770 | {
|
---|
| 1771 | stream << "<BoundingBoxes>" << endl;
|
---|
| 1772 |
|
---|
| 1773 | if (mObjectSpaceSubdivisionType == KD_BASED_OBJ_SUBDIV)
|
---|
| 1774 | {
|
---|
| 1775 | KdIntersectableMap::const_iterator kit, kit_end = mOspTree->mKdIntersectables.end();
|
---|
| 1776 |
|
---|
| 1777 | int id = 0;
|
---|
| 1778 | for (kit = mOspTree->mKdIntersectables.begin(); kit != kit_end; ++ kit, ++ id)
|
---|
| 1779 | {
|
---|
| 1780 | Intersectable *obj = (*kit).second;
|
---|
| 1781 | const AxisAlignedBox3 box = obj->GetBox();
|
---|
| 1782 |
|
---|
| 1783 | obj->SetId(id);
|
---|
| 1784 |
|
---|
| 1785 | stream << "<BoundingBox" << " id=\"" << id << "\""
|
---|
| 1786 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
| 1787 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
|
---|
| 1788 | }
|
---|
| 1789 | }
|
---|
[2093] | 1790 | else if (mObjectSpaceSubdivisionType == BV_BASED_OBJ_SUBDIV)
|
---|
| 1791 | {
|
---|
| 1792 | // export bounding boxes
|
---|
| 1793 | vector<BvhNode *> nodes;
|
---|
| 1794 |
|
---|
| 1795 | // hack: should also expect interior nodes
|
---|
| 1796 | mBvHierarchy->CollectNodes(mBvHierarchy->GetRoot(), nodes);
|
---|
| 1797 |
|
---|
| 1798 | vector<BvhNode *>::const_iterator oit, oit_end = nodes.end();
|
---|
| 1799 |
|
---|
| 1800 | for (oit = nodes.begin(); oit != oit_end; ++ oit)
|
---|
| 1801 | {
|
---|
| 1802 | const AxisAlignedBox3 box = (*oit)->GetBox();
|
---|
| 1803 | const int id = (*oit)->GetId();
|
---|
| 1804 |
|
---|
| 1805 | stream << "<BoundingBox" << " id=\"" << (*oit)->GetId() << "\""
|
---|
| 1806 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
| 1807 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
|
---|
| 1808 | }
|
---|
| 1809 | }
|
---|
[1614] | 1810 | else
|
---|
| 1811 | {
|
---|
| 1812 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 1813 |
|
---|
| 1814 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 1815 | {
|
---|
| 1816 | const AxisAlignedBox3 box = (*oit)->GetBox();
|
---|
| 1817 |
|
---|
| 1818 | stream << "<BoundingBox" << " id=\"" << (*oit)->GetId() << "\""
|
---|
| 1819 | << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
|
---|
| 1820 | << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\" />" << endl;
|
---|
| 1821 | }
|
---|
| 1822 | }
|
---|
| 1823 |
|
---|
| 1824 | stream << "</BoundingBoxes>" << endl;
|
---|
| 1825 | }
|
---|
| 1826 |
|
---|
[1680] | 1827 |
|
---|
[1706] | 1828 | class HierarchyNodeWrapper;
|
---|
| 1829 |
|
---|
| 1830 |
|
---|
| 1831 | template <typename T> class myless
|
---|
| 1832 | {
|
---|
| 1833 | public:
|
---|
| 1834 | bool operator() (T v1, T v2) const
|
---|
| 1835 | {
|
---|
| 1836 | return (v1->GetMergeCost() < v2->GetMergeCost());
|
---|
| 1837 | }
|
---|
| 1838 | };
|
---|
| 1839 |
|
---|
[1709] | 1840 |
|
---|
[1706] | 1841 | typedef priority_queue<HierarchyNodeWrapper *, vector<HierarchyNodeWrapper *>,
|
---|
| 1842 | myless<vector<HierarchyNodeWrapper *>::value_type> > HierarchyNodeQueue;
|
---|
| 1843 |
|
---|
| 1844 | class HierarchyNodeWrapper
|
---|
| 1845 | {
|
---|
| 1846 | public:
|
---|
[1707] | 1847 | enum {VSP_NODE, BVH_NODE, VIEW_CELL};
|
---|
[1706] | 1848 |
|
---|
| 1849 | virtual float GetMergeCost() const = 0;
|
---|
| 1850 | virtual int Type() const = 0;
|
---|
| 1851 | virtual bool IsLeaf() const = 0;
|
---|
| 1852 |
|
---|
| 1853 | virtual void PushChildren(HierarchyNodeQueue &tQueue) = 0;
|
---|
| 1854 | };
|
---|
| 1855 |
|
---|
| 1856 |
|
---|
| 1857 | class VspNodeWrapper: public HierarchyNodeWrapper
|
---|
| 1858 | {
|
---|
| 1859 | public:
|
---|
| 1860 | VspNodeWrapper(VspNode *node): mNode(node) {}
|
---|
| 1861 |
|
---|
| 1862 | int Type() const { return VSP_NODE; }
|
---|
| 1863 |
|
---|
| 1864 | float GetMergeCost() const { return (float) -mNode->mTimeStamp; };
|
---|
| 1865 |
|
---|
| 1866 | bool IsLeaf() const { return mNode->IsLeaf(); }
|
---|
| 1867 |
|
---|
| 1868 | void PushChildren(HierarchyNodeQueue &tQueue)
|
---|
| 1869 | {
|
---|
| 1870 | if (!mNode->IsLeaf())
|
---|
| 1871 | {
|
---|
[2017] | 1872 | VspInterior *interior = static_cast<VspInterior *>(mNode);
|
---|
[1706] | 1873 |
|
---|
| 1874 | tQueue.push(new VspNodeWrapper(interior->GetFront()));
|
---|
| 1875 | tQueue.push(new VspNodeWrapper(interior->GetBack()));
|
---|
| 1876 | }
|
---|
| 1877 | }
|
---|
| 1878 |
|
---|
| 1879 | VspNode *mNode;
|
---|
| 1880 | };
|
---|
| 1881 |
|
---|
| 1882 |
|
---|
| 1883 | class BvhNodeWrapper: public HierarchyNodeWrapper
|
---|
| 1884 | {
|
---|
| 1885 | public:
|
---|
| 1886 | BvhNodeWrapper(BvhNode *node): mNode(node) {}
|
---|
[1686] | 1887 |
|
---|
[1706] | 1888 | int Type() const { return BVH_NODE; }
|
---|
| 1889 |
|
---|
[1763] | 1890 | float GetMergeCost() const { return (float)-mNode->GetTimeStamp(); };
|
---|
[1706] | 1891 |
|
---|
| 1892 | bool IsLeaf() const { return mNode->IsLeaf(); }
|
---|
| 1893 |
|
---|
| 1894 | void PushChildren(HierarchyNodeQueue &tQueue)
|
---|
| 1895 | {
|
---|
| 1896 | if (!mNode->IsLeaf())
|
---|
| 1897 | {
|
---|
[2017] | 1898 | BvhInterior *interior = static_cast<BvhInterior *>(mNode);
|
---|
[1706] | 1899 |
|
---|
| 1900 | tQueue.push(new BvhNodeWrapper(interior->GetFront()));
|
---|
| 1901 | tQueue.push(new BvhNodeWrapper(interior->GetBack()));
|
---|
| 1902 | }
|
---|
| 1903 | }
|
---|
| 1904 |
|
---|
| 1905 | BvhNode *mNode;
|
---|
| 1906 | };
|
---|
| 1907 |
|
---|
| 1908 |
|
---|
[1707] | 1909 | class ViewCellWrapper: public HierarchyNodeWrapper
|
---|
| 1910 | {
|
---|
| 1911 | public:
|
---|
| 1912 |
|
---|
| 1913 | ViewCellWrapper(ViewCell *vc): mViewCell(vc) {}
|
---|
| 1914 |
|
---|
| 1915 | int Type() const { return VIEW_CELL; }
|
---|
| 1916 |
|
---|
| 1917 | float GetMergeCost() const { return mViewCell->GetMergeCost(); };
|
---|
| 1918 |
|
---|
| 1919 | bool IsLeaf() const { return mViewCell->IsLeaf(); }
|
---|
| 1920 |
|
---|
| 1921 | void PushChildren(HierarchyNodeQueue &tQueue)
|
---|
| 1922 | {
|
---|
| 1923 | if (!mViewCell->IsLeaf())
|
---|
| 1924 | {
|
---|
[2017] | 1925 | ViewCellInterior *interior = static_cast<ViewCellInterior *>(mViewCell);
|
---|
[1707] | 1926 |
|
---|
| 1927 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 1928 |
|
---|
| 1929 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 1930 | {
|
---|
| 1931 | tQueue.push(new ViewCellWrapper(*it));
|
---|
| 1932 | }
|
---|
| 1933 | }
|
---|
| 1934 | }
|
---|
| 1935 |
|
---|
| 1936 | ViewCell *mViewCell;
|
---|
| 1937 | };
|
---|
| 1938 |
|
---|
| 1939 |
|
---|
[1706] | 1940 | void HierarchyManager::CollectBestSet(const int maxSplits,
|
---|
| 1941 | const float maxMemoryCost,
|
---|
[1707] | 1942 | ViewCellContainer &viewCells,
|
---|
[1706] | 1943 | vector<BvhNode *> &bvhNodes)
|
---|
| 1944 | {
|
---|
| 1945 | HierarchyNodeQueue tqueue;
|
---|
[1707] | 1946 | //tqueue.push(new VspNodeWrapper(mVspTree->GetRoot()));
|
---|
| 1947 | tqueue.push(new ViewCellWrapper(mVspTree->mViewCellsTree->GetRoot()));
|
---|
[1706] | 1948 | tqueue.push(new BvhNodeWrapper(mBvHierarchy->GetRoot()));
|
---|
| 1949 |
|
---|
| 1950 | float memCost = 0;
|
---|
| 1951 |
|
---|
| 1952 | while (!tqueue.empty())
|
---|
| 1953 | {
|
---|
| 1954 | HierarchyNodeWrapper *nodeWrapper = tqueue.top();
|
---|
| 1955 | tqueue.pop();
|
---|
[1713] | 1956 | //cout << "priority: " << nodeWrapper->GetMergeCost() << endl;
|
---|
[1706] | 1957 | // save the view cells if it is a leaf or if enough view cells have already been traversed
|
---|
[2187] | 1958 | // because of the priority queue, this will be the optimal set of view cells
|
---|
[1706] | 1959 | if (nodeWrapper->IsLeaf() ||
|
---|
[1707] | 1960 | ((viewCells.size() + bvhNodes.size() + tqueue.size() + 1) >= maxSplits) ||
|
---|
[1706] | 1961 | (memCost > maxMemoryCost)
|
---|
| 1962 | )
|
---|
| 1963 | {
|
---|
[1713] | 1964 | if (nodeWrapper->Type() == HierarchyNodeWrapper::VIEW_CELL)
|
---|
[1706] | 1965 | {
|
---|
[1713] | 1966 | //cout << "1";
|
---|
[2017] | 1967 | ViewCellWrapper *viewCellWrapper = static_cast<ViewCellWrapper *>(nodeWrapper);
|
---|
[1707] | 1968 | viewCells.push_back(viewCellWrapper->mViewCell);
|
---|
[1706] | 1969 | }
|
---|
| 1970 | else
|
---|
| 1971 | {
|
---|
[1713] | 1972 | //cout << "0";
|
---|
[2017] | 1973 | BvhNodeWrapper *bvhNodeWrapper = static_cast<BvhNodeWrapper *>(nodeWrapper);
|
---|
[1706] | 1974 | bvhNodes.push_back(bvhNodeWrapper->mNode);
|
---|
| 1975 | }
|
---|
| 1976 | }
|
---|
| 1977 | else
|
---|
| 1978 | {
|
---|
| 1979 | nodeWrapper->PushChildren(tqueue);
|
---|
| 1980 | }
|
---|
| 1981 |
|
---|
| 1982 | delete nodeWrapper;
|
---|
| 1983 | }
|
---|
| 1984 | }
|
---|
| 1985 |
|
---|
| 1986 |
|
---|
[1842] | 1987 | void HierarchyManager::ComputePvs(const ObjectPvs &pvs,
|
---|
| 1988 | float &rc,
|
---|
| 1989 | int &pvsEntries)
|
---|
[1745] | 1990 | {
|
---|
| 1991 | BvhNode::NewMail();
|
---|
| 1992 |
|
---|
| 1993 | ObjectPvsIterator pit = pvs.GetIterator();
|
---|
| 1994 |
|
---|
| 1995 | while (pit.HasMoreEntries())
|
---|
| 1996 | {
|
---|
[2117] | 1997 | Intersectable *obj = pit.Next();
|
---|
[1745] | 1998 |
|
---|
[2117] | 1999 | if (obj->Type() != Intersectable::BVH_INTERSECTABLE)
|
---|
[2187] | 2000 | {
|
---|
| 2001 | cout << "error: wrong object type detected: " << obj->Type() << endl;
|
---|
| 2002 | exit(0);
|
---|
| 2003 | }
|
---|
[1842] | 2004 |
|
---|
[2117] | 2005 | BvhNode *intersect = static_cast<BvhNode *>(obj);
|
---|
[1758] | 2006 | BvhNode *activeNode;
|
---|
[1745] | 2007 |
|
---|
[1758] | 2008 | // hack for choosing which node to account for
|
---|
| 2009 | if (intersect->IsLeaf())
|
---|
[1786] | 2010 | {
|
---|
[1842] | 2011 | activeNode =
|
---|
[2017] | 2012 | static_cast<BvhLeaf *>(intersect)->GetActiveNode();
|
---|
[1786] | 2013 | }
|
---|
[1758] | 2014 | else
|
---|
[1786] | 2015 | {
|
---|
[1758] | 2016 | activeNode = intersect;
|
---|
[1786] | 2017 | }
|
---|
[1745] | 2018 |
|
---|
| 2019 | if (!activeNode->Mailed())
|
---|
| 2020 | {
|
---|
| 2021 | activeNode->Mail();
|
---|
| 2022 |
|
---|
[1786] | 2023 | #if STUPID_METHOD
|
---|
| 2024 |
|
---|
[1745] | 2025 | ObjectContainer objects;
|
---|
[1786] | 2026 | activeNode->CollectObjects(objects);
|
---|
| 2027 | rc += mBvHierarchy->EvalAbsCost(objects);
|
---|
| 2028 | #else
|
---|
| 2029 | rc += mBvHierarchy->GetRenderCostIncrementially(activeNode);
|
---|
| 2030 | #endif
|
---|
[1745] | 2031 | ++ pvsEntries;
|
---|
| 2032 | }
|
---|
| 2033 | }
|
---|
| 2034 | }
|
---|
| 2035 |
|
---|
| 2036 |
|
---|
[1787] | 2037 | void HierarchyManager::GetPvsEfficiently(ViewCell *viewCell, ObjectPvs &pvs) const
|
---|
| 2038 | {
|
---|
| 2039 | ////////////////
|
---|
| 2040 | //-- pvs is not stored with the interiors => reconstruct
|
---|
| 2041 |
|
---|
| 2042 | // add pvs from leaves
|
---|
| 2043 | stack<ViewCell *> tstack;
|
---|
| 2044 | tstack.push(viewCell);
|
---|
| 2045 |
|
---|
| 2046 | Intersectable::NewMail();
|
---|
| 2047 |
|
---|
| 2048 | while (!tstack.empty())
|
---|
| 2049 | {
|
---|
| 2050 | ViewCell *vc = tstack.top();
|
---|
| 2051 | tstack.pop();
|
---|
| 2052 |
|
---|
| 2053 | // add newly found pvs to merged pvs: break here even for interior
|
---|
| 2054 | if (!vc->GetPvs().Empty())
|
---|
| 2055 | {
|
---|
| 2056 | ObjectPvsIterator pit = vc->GetPvs().GetIterator();
|
---|
| 2057 |
|
---|
| 2058 | while (pit.HasMoreEntries())
|
---|
| 2059 | {
|
---|
[2117] | 2060 | Intersectable *object = pit.Next();
|
---|
[1787] | 2061 |
|
---|
| 2062 | if (!object->Mailed())
|
---|
| 2063 | {
|
---|
| 2064 | object->Mail();
|
---|
| 2065 | pvs.AddSampleDirty(object, 1.0f);
|
---|
| 2066 | }
|
---|
| 2067 | }
|
---|
| 2068 | }
|
---|
| 2069 | else if (!vc->IsLeaf()) // interior cells: go down to leaf level
|
---|
| 2070 | {
|
---|
[2017] | 2071 | ViewCellInterior *interior = static_cast<ViewCellInterior *>(vc);
|
---|
[1787] | 2072 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 2073 |
|
---|
| 2074 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 2075 | {
|
---|
| 2076 | tstack.push(*it);
|
---|
| 2077 | }
|
---|
| 2078 | }
|
---|
| 2079 | }
|
---|
| 2080 | }
|
---|
| 2081 |
|
---|
| 2082 |
|
---|
[1750] | 2083 | // TODO matt: implement this function for different storing methods
|
---|
[1787] | 2084 | void HierarchyManager::GetPvsIncrementially(ViewCell *vc, ObjectPvs &pvs) const
|
---|
[1750] | 2085 | {
|
---|
| 2086 | ////////////////
|
---|
| 2087 | //-- pvs is not stored with the interiors => reconstruct
|
---|
| 2088 |
|
---|
| 2089 | // add pvs from leaves
|
---|
| 2090 | stack<ViewCell *> tstack;
|
---|
| 2091 | tstack.push(vc);
|
---|
| 2092 |
|
---|
| 2093 | while (!tstack.empty())
|
---|
| 2094 | {
|
---|
| 2095 | vc = tstack.top();
|
---|
| 2096 | tstack.pop();
|
---|
| 2097 |
|
---|
| 2098 | // add newly found pvs to merged pvs: break here even for interior
|
---|
| 2099 | if (!vc->GetPvs().Empty())
|
---|
| 2100 | {
|
---|
| 2101 | pvs.MergeInPlace(vc->GetPvs());
|
---|
| 2102 | }
|
---|
| 2103 | else if (!vc->IsLeaf()) // interior cells: go down to leaf level
|
---|
| 2104 | {
|
---|
[2017] | 2105 | ViewCellInterior *interior = static_cast<ViewCellInterior *>(vc);
|
---|
[1750] | 2106 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 2107 |
|
---|
| 2108 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 2109 | {
|
---|
| 2110 | tstack.push(*it);
|
---|
| 2111 | }
|
---|
| 2112 | }
|
---|
| 2113 | }
|
---|
| 2114 | }
|
---|
| 2115 |
|
---|
[1786] | 2116 |
|
---|
| 2117 | // TODO matt: implement this function for different storing methods
|
---|
| 2118 | void HierarchyManager::PullUpPvsIncrementally(ViewCell *viewCell) const
|
---|
| 2119 | {
|
---|
| 2120 | ////////////////
|
---|
| 2121 | //-- pvs is not stored with the interiors => reconstruct
|
---|
| 2122 |
|
---|
| 2123 | // early exit: pvs is already pulled up to this view cell
|
---|
| 2124 | if (!viewCell->GetPvs().Empty())
|
---|
| 2125 | return;
|
---|
| 2126 |
|
---|
| 2127 | // add pvs from leaves
|
---|
| 2128 | stack<ViewCell *> tstack;
|
---|
| 2129 | tstack.push(viewCell);
|
---|
| 2130 |
|
---|
| 2131 | ViewCell *vc = viewCell;
|
---|
| 2132 |
|
---|
| 2133 | while (!tstack.empty())
|
---|
| 2134 | {
|
---|
| 2135 | vc = tstack.top();
|
---|
| 2136 | tstack.pop();
|
---|
| 2137 |
|
---|
| 2138 | // add newly found pvs to merged pvs: break here even for interior
|
---|
| 2139 | if (!vc->GetPvs().Empty())
|
---|
| 2140 | {
|
---|
[1787] | 2141 | viewCell->GetPvs().MergeInPlace(vc->GetPvs());
|
---|
[1786] | 2142 | }
|
---|
| 2143 | else if (!vc->IsLeaf()) // interior cells: go down to leaf level
|
---|
| 2144 | {
|
---|
| 2145 | //cout <<" t";
|
---|
[2017] | 2146 | ViewCellInterior *interior = static_cast<ViewCellInterior *>(vc);
|
---|
[1786] | 2147 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 2148 |
|
---|
| 2149 | for (it = interior->mChildren.begin(); it != it_end; ++ it)
|
---|
| 2150 | {
|
---|
| 2151 | tstack.push(*it);
|
---|
| 2152 | }
|
---|
| 2153 | }
|
---|
| 2154 | }
|
---|
| 2155 | }
|
---|
| 2156 |
|
---|
| 2157 |
|
---|
| 2158 |
|
---|
| 2159 | // TODO matt: implement this function for different storing methods
|
---|
| 2160 | void HierarchyManager::GetPvsRecursive(ViewCell *vc, ObjectPvs &pvs) const
|
---|
| 2161 | {
|
---|
| 2162 | ////////////////
|
---|
| 2163 | //-- pvs is not stored with the interiors => reconstruct
|
---|
| 2164 | if (vc->IsLeaf() || !vc->GetPvs().Empty())
|
---|
| 2165 | {
|
---|
| 2166 | pvs = vc->GetPvs();
|
---|
| 2167 | }
|
---|
| 2168 | else
|
---|
| 2169 | {
|
---|
[2017] | 2170 | ViewCellInterior *interior = static_cast<ViewCellInterior *>(vc);
|
---|
[1786] | 2171 | #if 0
|
---|
| 2172 | ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
|
---|
| 2173 | const int childPvsSize = (int)interior->mChildren.size();
|
---|
| 2174 | vector<ObjectPvs> childPvs;
|
---|
| 2175 | childPvs.resize((int)interior->mChildren.size());
|
---|
| 2176 |
|
---|
| 2177 | int i = 0;
|
---|
| 2178 | for (it = interior->mChildren.begin(); it != it_end; ++ it, ++ i)
|
---|
| 2179 | {
|
---|
| 2180 | GetPvsRecursive(*it, childPvs[i]);
|
---|
| 2181 | pvs.MergeInPlace(childPvs[i]);
|
---|
| 2182 | }
|
---|
| 2183 | #else
|
---|
| 2184 |
|
---|
| 2185 | ObjectPvs leftPvs, rightPvs;
|
---|
| 2186 |
|
---|
| 2187 | GetPvsRecursive(interior->mChildren[0], leftPvs);
|
---|
| 2188 | GetPvsRecursive(interior->mChildren[1], rightPvs);
|
---|
| 2189 |
|
---|
| 2190 | ObjectPvs::Merge(pvs, leftPvs, rightPvs);
|
---|
| 2191 | #endif
|
---|
| 2192 | }
|
---|
| 2193 | }
|
---|
| 2194 |
|
---|
| 2195 |
|
---|
[1713] | 2196 | int HierarchyManager::ExtractStatistics(const int maxSplits,
|
---|
| 2197 | const float maxMemoryCost,
|
---|
| 2198 | float &renderCost,
|
---|
| 2199 | float &memory,
|
---|
[1718] | 2200 | int &pvsEntries,
|
---|
| 2201 | int &viewSpaceSplits,
|
---|
[1745] | 2202 | int &objectSpaceSplits,
|
---|
[1919] | 2203 | const bool useFilter,
|
---|
| 2204 | const bool useHisto,
|
---|
| 2205 | const int histoMem,
|
---|
| 2206 | const int pass,
|
---|
| 2207 | bool &histoUsed)
|
---|
[1706] | 2208 | {
|
---|
[1707] | 2209 | ViewCellContainer viewCells;
|
---|
| 2210 | vector<BvhNode *> bvhNodes;
|
---|
| 2211 |
|
---|
[1709] | 2212 | // collect best set of view cells for this #splits
|
---|
[1707] | 2213 | CollectBestSet(maxSplits, maxMemoryCost, viewCells, bvhNodes);
|
---|
| 2214 | vector<BvhNode *>::const_iterator bit, bit_end = bvhNodes.end();
|
---|
| 2215 |
|
---|
[1713] | 2216 | // set new nodes to be active
|
---|
[1707] | 2217 | for (bit = bvhNodes.begin(); bit != bit_end; ++ bit)
|
---|
| 2218 | {
|
---|
| 2219 | mBvHierarchy->SetActive(*bit);
|
---|
| 2220 | }
|
---|
| 2221 |
|
---|
| 2222 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
| 2223 |
|
---|
[1713] | 2224 | pvsEntries = 0;
|
---|
| 2225 | renderCost = 0.0f;
|
---|
[1707] | 2226 |
|
---|
[1750] | 2227 | ViewCell::NewMail();
|
---|
| 2228 |
|
---|
[1787] | 2229 | //cout << "\nviewcells: " << viewCells.size() << endl;
|
---|
[1707] | 2230 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
| 2231 | {
|
---|
| 2232 | ViewCell *vc = *vit;
|
---|
[1750] | 2233 | float rc = 0;
|
---|
[1786] | 2234 |
|
---|
| 2235 | #if STUPID_METHOD
|
---|
| 2236 | ObjectPvs pvs;
|
---|
[1787] | 2237 | GetPvsIncrementially(vc, pvs);
|
---|
[1750] | 2238 | vc->SetPvs(pvs);
|
---|
[1787] | 2239 |
|
---|
[1786] | 2240 | #else
|
---|
[1787] | 2241 |
|
---|
[1786] | 2242 | ObjectPvs pvs;
|
---|
[1789] | 2243 |
|
---|
[1787] | 2244 | if (vc->GetPvs().Empty())
|
---|
| 2245 | {
|
---|
[1789] | 2246 | // warning: uses mailing, pvs not sorted!!
|
---|
[1787] | 2247 | GetPvsEfficiently(vc, pvs);
|
---|
[1789] | 2248 | //GetPvsRecursive(vc, pvs);
|
---|
[1787] | 2249 | vc->SetPvs(pvs);
|
---|
| 2250 | }
|
---|
[1786] | 2251 | #endif
|
---|
[1750] | 2252 |
|
---|
| 2253 | vc->Mail();
|
---|
| 2254 |
|
---|
[1745] | 2255 | if (useFilter)
|
---|
[1707] | 2256 | {
|
---|
[1786] | 2257 | const long startT = GetTime();
|
---|
[1745] | 2258 | ObjectPvs filteredPvs;
|
---|
[1750] | 2259 | mVspTree->mViewCellsManager->ApplyFilter2(vc, false, 1.0f, filteredPvs);
|
---|
[1786] | 2260 | const long endT = GetTime();
|
---|
| 2261 |
|
---|
[1787] | 2262 | //cout << "filter computed in " << TimeDiff(startT, endT) * 1e-3f << " secs" << endl;
|
---|
[1745] | 2263 | ComputePvs(filteredPvs, rc, pvsEntries);
|
---|
[1707] | 2264 | }
|
---|
[1745] | 2265 | else
|
---|
| 2266 | {
|
---|
[1786] | 2267 | ComputePvs(vc->GetPvs(), rc, pvsEntries);
|
---|
[1919] | 2268 | vc->SetPvsCost(rc);
|
---|
[1749] | 2269 | }
|
---|
[1713] | 2270 |
|
---|
| 2271 | rc *= vc->GetVolume();
|
---|
| 2272 | renderCost += rc;
|
---|
[1707] | 2273 | }
|
---|
| 2274 |
|
---|
[1713] | 2275 | renderCost /= mVspTree->mViewCellsManager->GetViewSpaceBox().GetVolume();
|
---|
| 2276 | memory = pvsEntries * ObjectPvs::GetEntrySize();
|
---|
[1718] | 2277 |
|
---|
| 2278 | viewSpaceSplits = (int)viewCells.size();
|
---|
| 2279 | objectSpaceSplits = (int)bvhNodes.size();
|
---|
[1919] | 2280 |
|
---|
| 2281 | ////////////////////////
|
---|
| 2282 | //-- evaluate histogram for pvs size
|
---|
| 2283 |
|
---|
| 2284 | if (useHisto && (memory <= (float)histoMem))
|
---|
| 2285 | {
|
---|
| 2286 | char str[100];
|
---|
| 2287 | char statsPrefix[100];
|
---|
[1933] | 2288 | //int histoStepSize;
|
---|
[1919] | 2289 |
|
---|
| 2290 | Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.statsPrefix", statsPrefix);
|
---|
| 2291 |
|
---|
| 2292 | cout << "computing pvs histogram for " << histoMem << " memory" << endl;
|
---|
| 2293 | Debug << "computing pvs histogram for " << histoMem << " memory" << endl;
|
---|
| 2294 |
|
---|
| 2295 | sprintf(str, "-%05d-%05d-histo-pvs.log", pass, histoMem);
|
---|
| 2296 | const string filename = string(statsPrefix) + string(str);
|
---|
| 2297 |
|
---|
| 2298 | mVspTree->mViewCellsManager->EvalViewCellHistogramForPvsSize(filename, viewCells);
|
---|
| 2299 |
|
---|
| 2300 | histoUsed = true;
|
---|
| 2301 | }
|
---|
| 2302 |
|
---|
[1713] | 2303 | //cout << "viewCells: " << (int)viewCells.size() << " nodes: " << (int)bvhNodes.size() << " rc: " << renderCost << " entries: " << pvsEntries << endl;
|
---|
| 2304 |
|
---|
[1750] | 2305 | // delete old "base" view cells if they are not leaves
|
---|
| 2306 | ViewCellContainer::const_iterator oit, oit_end = mOldViewCells.end();
|
---|
| 2307 |
|
---|
| 2308 | for (oit = mOldViewCells.begin(); oit != oit_end; ++ oit)
|
---|
| 2309 | {
|
---|
| 2310 | if (!(*oit)->Mailed() && !(*oit)->IsLeaf())
|
---|
| 2311 | {
|
---|
| 2312 | (*oit)->GetPvs().Clear();
|
---|
| 2313 | }
|
---|
| 2314 | }
|
---|
| 2315 |
|
---|
[1786] | 2316 | // store current level
|
---|
[1750] | 2317 | mOldViewCells = viewCells;
|
---|
| 2318 |
|
---|
[1764] | 2319 | return (int)(viewCells.size() + bvhNodes.size());
|
---|
[1709] | 2320 | }
|
---|
[1706] | 2321 |
|
---|
| 2322 |
|
---|
[1709] | 2323 | void HierarchyManager::ExportStats(ofstream &stats,
|
---|
| 2324 | SplitQueue &tQueue,
|
---|
| 2325 | const ObjectContainer &objects)
|
---|
| 2326 | {
|
---|
[1723] | 2327 | HierarchySubdivisionStats subStats;
|
---|
| 2328 | subStats.Reset();
|
---|
[1706] | 2329 |
|
---|
| 2330 | /////////////
|
---|
[1723] | 2331 | //-- initial situation
|
---|
[1706] | 2332 |
|
---|
[1723] | 2333 | subStats.mNumSplits = 0;
|
---|
| 2334 | subStats.mTotalRenderCost = (float)objects.size();
|
---|
| 2335 | subStats.mEntriesInPvs = 1;
|
---|
| 2336 | subStats.mMemoryCost = (float)ObjectPvs::GetEntrySize();
|
---|
| 2337 | subStats.mFullMemory = subStats.mMemoryCost;
|
---|
| 2338 | subStats.mViewSpaceSplits = 0;
|
---|
| 2339 | subStats.mObjectSpaceSplits = 0;
|
---|
[1744] | 2340 | subStats.mRenderCostDecrease = 0;
|
---|
[1723] | 2341 | subStats.Print(stats);
|
---|
[1706] | 2342 |
|
---|
[1723] | 2343 | cout << "exporting vsposp stats ... " << endl;
|
---|
| 2344 |
|
---|
[1706] | 2345 | //-- go through tree in the order of render cost decrease
|
---|
| 2346 | //-- which is the same order as the view cells were merged
|
---|
| 2347 | //-- or the reverse order of subdivision for subdivision-only
|
---|
| 2348 | //-- view cell hierarchies.
|
---|
[1789] | 2349 |
|
---|
[1709] | 2350 | while (!tQueue.Empty())
|
---|
[1706] | 2351 | {
|
---|
[1709] | 2352 | SubdivisionCandidate *nextCandidate = NextSubdivisionCandidate(tQueue);
|
---|
| 2353 | bool isLeaf;
|
---|
| 2354 | int timeStamp;
|
---|
[1933] | 2355 | //float rcDecr;
|
---|
| 2356 | //int entriesIncr;
|
---|
[1706] | 2357 |
|
---|
[1789] | 2358 | if (nextCandidate->Type() == SubdivisionCandidate::VIEW_SPACE)
|
---|
[1709] | 2359 | {
|
---|
| 2360 | timeStamp = (int)-nextCandidate->GetPriority();
|
---|
[1706] | 2361 |
|
---|
[1709] | 2362 | VspNode *newNode = mVspTree->SubdivideAndCopy(tQueue, nextCandidate);
|
---|
| 2363 | VspNode *oldNode = (VspNode *)nextCandidate->mEvaluationHack;
|
---|
[1789] | 2364 |
|
---|
[1709] | 2365 | isLeaf = newNode->IsLeaf();
|
---|
[1732] | 2366 | //subStats.mRenderCostDecrease = oldNode->mRenderCostDecr;
|
---|
| 2367 | //entriesIncr = oldNode->mPvsEntriesIncr;
|
---|
[1709] | 2368 | }
|
---|
| 2369 | else
|
---|
| 2370 | {
|
---|
| 2371 | timeStamp = (int)-nextCandidate->GetPriority();
|
---|
[1789] | 2372 |
|
---|
[1709] | 2373 | BvhNode *newNode = mBvHierarchy->SubdivideAndCopy(tQueue, nextCandidate);
|
---|
| 2374 | BvhNode *oldNode = (BvhNode *)nextCandidate->mEvaluationHack;
|
---|
[1789] | 2375 |
|
---|
[1709] | 2376 | isLeaf = newNode->IsLeaf();
|
---|
[1732] | 2377 | //subStats.mRenderCostDecrease = oldNode->mRenderCostDecr;
|
---|
| 2378 | //entriesIncr = oldNode->mPvsEntriesIncr;
|
---|
[1709] | 2379 | }
|
---|
[1789] | 2380 |
|
---|
[1709] | 2381 | if (!isLeaf)
|
---|
| 2382 | {
|
---|
[1723] | 2383 | subStats.mTotalRenderCost -= subStats.mRenderCostDecrease;
|
---|
[1738] | 2384 | //subStats.mEntriesInPvs += entriesIncr;
|
---|
[1723] | 2385 |
|
---|
[1718] | 2386 | if (nextCandidate->Type() == SubdivisionCandidate::VIEW_SPACE)
|
---|
| 2387 | {
|
---|
[1723] | 2388 | ++ subStats.mViewSpaceSplits;
|
---|
[2227] | 2389 | //cout << "v";
|
---|
[1741] | 2390 | //cout << "vsp t: " << timeStamp << " rc: " << rcDecr << " pvs: " << entriesIncr << endl;
|
---|
[1718] | 2391 | }
|
---|
[1709] | 2392 | else
|
---|
[1718] | 2393 | {
|
---|
[1723] | 2394 | ++ subStats.mObjectSpaceSplits;
|
---|
[2227] | 2395 | //cout << "o";
|
---|
[1741] | 2396 | //"osp t: " << timeStamp << " rc: " << rcDecr << " pvs: " << entriesIncr << endl;
|
---|
[1718] | 2397 | }
|
---|
[1706] | 2398 |
|
---|
[1723] | 2399 | ++ subStats.mNumSplits;
|
---|
[1706] | 2400 |
|
---|
[1723] | 2401 | if ((subStats.mNumSplits % 500) == 499)
|
---|
| 2402 | cout << subStats.mNumSplits << " steps taken" << endl;
|
---|
[1718] | 2403 |
|
---|
[1723] | 2404 | subStats.mMemoryCost = (float)subStats.mEntriesInPvs * (float)ObjectPvs::GetEntrySize();
|
---|
| 2405 | subStats.mFullMemory = subStats.mMemoryCost;
|
---|
| 2406 |
|
---|
| 2407 | subStats.Print(stats);
|
---|
[1789] | 2408 |
|
---|
[1709] | 2409 | }
|
---|
[1706] | 2410 |
|
---|
[1709] | 2411 | DEL_PTR(nextCandidate);
|
---|
| 2412 | }
|
---|
[1706] | 2413 |
|
---|
[1709] | 2414 | stats.close();
|
---|
| 2415 | }
|
---|
[1706] | 2416 |
|
---|
| 2417 |
|
---|
[1741] | 2418 | void HierarchyManager::EvaluateSubdivision(const VssRayContainer &sampleRays,
|
---|
[1709] | 2419 | const ObjectContainer &objects,
|
---|
| 2420 | const string &filename)
|
---|
| 2421 | {
|
---|
[1779] | 2422 | #if 0
|
---|
[1709] | 2423 | VspTree *oldVspTree = mVspTree;
|
---|
| 2424 | ViewCellsManager *vm = mVspTree->mViewCellsManager;
|
---|
| 2425 | BvHierarchy *oldHierarchy = mBvHierarchy;
|
---|
[1706] | 2426 |
|
---|
[1709] | 2427 | mBvHierarchy = new BvHierarchy();
|
---|
| 2428 | mBvHierarchy->mHierarchyManager = this;
|
---|
| 2429 | mBvHierarchy->mViewCellsManager = vm;
|
---|
[1706] | 2430 |
|
---|
[1709] | 2431 | mVspTree = new VspTree();
|
---|
| 2432 | mVspTree->mHierarchyManager = this;
|
---|
| 2433 | mVspTree->mViewCellsManager = vm;
|
---|
[1706] | 2434 |
|
---|
[1709] | 2435 | // create first nodes
|
---|
| 2436 | mVspTree->Initialise(sampleRays, &oldVspTree->mBoundingBox);
|
---|
| 2437 | InitialiseObjectSpaceSubdivision(objects);
|
---|
[1706] | 2438 |
|
---|
[1709] | 2439 | const long startTime = GetTime();
|
---|
| 2440 | cout << "Constructing evaluation hierarchies ... \n";
|
---|
| 2441 |
|
---|
| 2442 | ofstream stats;
|
---|
| 2443 | stats.open(filename.c_str());
|
---|
| 2444 | SplitQueue tQueue;
|
---|
[1706] | 2445 |
|
---|
[1709] | 2446 | BvhNode *oldBvhRoot = oldHierarchy->GetRoot();
|
---|
| 2447 | VspNode *oldVspRoot = oldVspTree->GetRoot();
|
---|
[1706] | 2448 |
|
---|
[1709] | 2449 | RayInfoContainer *viewSpaceRays = new RayInfoContainer();
|
---|
| 2450 |
|
---|
| 2451 | SubdivisionCandidate *firstVsp = mVspTree->PrepareConstruction(sampleRays, *viewSpaceRays);
|
---|
[1784] | 2452 | tQueue.Push(firstVsp);
|
---|
| 2453 |
|
---|
[1779] | 2454 | mBvHierarchy->PrepareConstruction(tQueue, sampleRays, objects);
|
---|
[1709] | 2455 |
|
---|
| 2456 | firstVsp->mEvaluationHack = oldVspRoot;
|
---|
| 2457 | firstBvh->mEvaluationHack = oldBvhRoot;
|
---|
| 2458 |
|
---|
| 2459 | firstVsp->SetPriority((float)-oldVspRoot->mTimeStamp);
|
---|
[1763] | 2460 | firstBvh->SetPriority((float)-oldBvhRoot->GetTimeStamp());
|
---|
[1779] | 2461 |
|
---|
[1709] | 2462 | ExportStats(stats, tQueue, objects);
|
---|
| 2463 |
|
---|
| 2464 | cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
| 2465 | RemoveRayRefs(objects);
|
---|
| 2466 |
|
---|
| 2467 | // view cells needed only for evaluation
|
---|
| 2468 | ViewCellContainer viewCells;
|
---|
| 2469 | mVspTree->CollectViewCells(viewCells, false);
|
---|
| 2470 |
|
---|
| 2471 | // helper trees can be destroyed
|
---|
| 2472 | DEL_PTR(mVspTree);
|
---|
| 2473 | DEL_PTR(mBvHierarchy);
|
---|
| 2474 |
|
---|
| 2475 | CLEAR_CONTAINER(viewCells);
|
---|
| 2476 |
|
---|
| 2477 | // reset hierarchies
|
---|
| 2478 | mVspTree = oldVspTree;
|
---|
| 2479 | mBvHierarchy = oldHierarchy;
|
---|
| 2480 |
|
---|
| 2481 | // reinstall old bv refs
|
---|
| 2482 | vector<BvhLeaf *> leaves;
|
---|
| 2483 | mBvHierarchy->CollectLeaves(mBvHierarchy->GetRoot(), leaves);
|
---|
| 2484 | vector<BvhLeaf *>::const_iterator bit, bit_end = leaves.end();
|
---|
| 2485 |
|
---|
| 2486 | for (bit = leaves.begin(); bit != bit_end; ++ bit)
|
---|
| 2487 | {
|
---|
| 2488 | mBvHierarchy->AssociateObjectsWithLeaf(*bit);
|
---|
[1706] | 2489 | }
|
---|
[1779] | 2490 | #endif
|
---|
[1709] | 2491 | }
|
---|
[1706] | 2492 |
|
---|
[1709] | 2493 |
|
---|
| 2494 | void HierarchyManager::EvaluateSubdivision2(ofstream &splitsStats,
|
---|
[1745] | 2495 | const int splitsStepSize,
|
---|
[1919] | 2496 | const bool useFilter,
|
---|
| 2497 | const bool useHisto,
|
---|
| 2498 | const int histoMem,
|
---|
| 2499 | const int pass)
|
---|
[1709] | 2500 | {
|
---|
[1750] | 2501 | vector<HierarchySubdivisionStats> subStatsContainer;
|
---|
[1709] | 2502 |
|
---|
[1786] | 2503 | int splits = (1 + (mHierarchyStats.Leaves() - 1) / splitsStepSize) * splitsStepSize;
|
---|
[1750] | 2504 | cout << "splits: " << splits << endl;
|
---|
| 2505 |
|
---|
[1919] | 2506 | bool histoUsed = false;
|
---|
| 2507 |
|
---|
[1709] | 2508 | while (1)
|
---|
| 2509 | {
|
---|
[1750] | 2510 | HierarchySubdivisionStats subStats;
|
---|
[1723] | 2511 | subStats.mNumSplits = ExtractStatistics(splits,
|
---|
[1718] | 2512 | 99999.0,
|
---|
[1723] | 2513 | subStats.mTotalRenderCost,
|
---|
| 2514 | subStats.mMemoryCost,
|
---|
| 2515 | subStats.mEntriesInPvs,
|
---|
| 2516 | subStats.mViewSpaceSplits,
|
---|
[1745] | 2517 | subStats.mObjectSpaceSplits,
|
---|
[1919] | 2518 | useFilter,
|
---|
| 2519 | useHisto && !histoUsed,
|
---|
| 2520 | histoMem,
|
---|
| 2521 | pass,
|
---|
| 2522 | histoUsed);
|
---|
[1723] | 2523 |
|
---|
[1713] | 2524 |
|
---|
[1723] | 2525 | const float objectSpaceHierarchyMem = float(
|
---|
[1760] | 2526 | subStats.mObjectSpaceSplits * mBvHierarchy->mMemoryConst//sizeof(ObjectContainer)
|
---|
[1732] | 2527 | //+ (subStats.mObjectSpaceSplits - 1) * sizeof(BvhInterior)
|
---|
| 2528 | //+sizeof(BvHierarchy)
|
---|
[1723] | 2529 | ) / float(1024 * 1024);
|
---|
[1718] | 2530 |
|
---|
[1723] | 2531 |
|
---|
| 2532 | const float viewSpaceHierarchyMem = float(
|
---|
[1760] | 2533 | subStats.mViewSpaceSplits * mVspTree->mMemoryConst//sizeof(ObjectPvs)
|
---|
[1732] | 2534 | //+ (subStats.mViewSpaceSplits - 1) * sizeof(VspInterior)
|
---|
| 2535 | + sizeof(ObjectPvs)
|
---|
| 2536 | //+ sizeof(VspTree)
|
---|
[1723] | 2537 | ) / float(1024 * 1024);
|
---|
| 2538 |
|
---|
| 2539 | subStats.mFullMemory = subStats.mMemoryCost + objectSpaceHierarchyMem + viewSpaceHierarchyMem;
|
---|
| 2540 |
|
---|
[1750] | 2541 | subStatsContainer.push_back(subStats);
|
---|
| 2542 |
|
---|
| 2543 | if (splits == 0)
|
---|
[1786] | 2544 | {
|
---|
[1709] | 2545 | break;
|
---|
[1786] | 2546 | }
|
---|
[1750] | 2547 | splits -= splitsStepSize;
|
---|
| 2548 |
|
---|
[1786] | 2549 | cout << "splits: " << subStats.mNumSplits << " ";
|
---|
[1709] | 2550 | }
|
---|
[1745] | 2551 |
|
---|
[1750] | 2552 | vector<HierarchySubdivisionStats>::const_reverse_iterator hit, hit_end = subStatsContainer.rend();
|
---|
| 2553 |
|
---|
| 2554 | for (hit = subStatsContainer.rbegin(); hit != hit_end; ++ hit)
|
---|
| 2555 | {
|
---|
| 2556 | (*hit).Print(splitsStats);
|
---|
| 2557 | }
|
---|
| 2558 |
|
---|
| 2559 | // delete old "base" view cells: only pvss in the leaves are allowed
|
---|
| 2560 | ViewCellContainer::const_iterator oit, oit_end = mOldViewCells.end();
|
---|
[2210] | 2561 |
|
---|
[1750] | 2562 | for (oit = mOldViewCells.begin(); oit != oit_end; ++ oit)
|
---|
| 2563 | {
|
---|
| 2564 | if (!(*oit)->IsLeaf())
|
---|
| 2565 | {
|
---|
| 2566 | (*oit)->GetPvs().Clear();
|
---|
| 2567 | }
|
---|
| 2568 | }
|
---|
| 2569 |
|
---|
| 2570 | mOldViewCells.clear();
|
---|
| 2571 |
|
---|
[1763] | 2572 | // reset active nodes
|
---|
| 2573 | vector<BvhLeaf *> bvhLeaves;
|
---|
| 2574 |
|
---|
| 2575 | mBvHierarchy->CollectLeaves(mBvHierarchy->GetRoot(), bvhLeaves);
|
---|
| 2576 |
|
---|
| 2577 | vector<BvhLeaf *>::const_iterator bit, bit_end = bvhLeaves.end();
|
---|
| 2578 |
|
---|
| 2579 | for (bit = bvhLeaves.begin(); bit != bit_end; ++ bit)
|
---|
| 2580 | {
|
---|
| 2581 | (*bit)->SetActiveNode(*bit);
|
---|
| 2582 | }
|
---|
| 2583 |
|
---|
[1732] | 2584 | cout << endl;
|
---|
[1706] | 2585 | }
|
---|
[1709] | 2586 |
|
---|
[1718] | 2587 |
|
---|
| 2588 | void HierarchyManager::CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects)
|
---|
| 2589 | {
|
---|
[1719] | 2590 | mBvHierarchy->CollectObjects(box, objects);
|
---|
[1715] | 2591 | }
|
---|
[1718] | 2592 |
|
---|
[1843] | 2593 |
|
---|
[1845] | 2594 | int HierarchyManager::CompressObjectSpace()
|
---|
[1844] | 2595 | {
|
---|
| 2596 | //mBvHierarchy->Compress();
|
---|
[1845] | 2597 | return mVspTree->CompressObjects();
|
---|
| 2598 | }
|
---|
[1844] | 2599 |
|
---|
[1845] | 2600 |
|
---|
[1843] | 2601 | void HierarchyManager::CreateUniqueObjectIds()
|
---|
| 2602 | {
|
---|
| 2603 | mBvHierarchy->CreateUniqueObjectIds();
|
---|
[1718] | 2604 | }
|
---|
[1843] | 2605 |
|
---|
| 2606 |
|
---|
[2073] | 2607 | void HierarchyManager::CreateTraversalTree()
|
---|
| 2608 | {
|
---|
[2170] | 2609 | int mind, maxd;
|
---|
| 2610 | mVspTree->EvalMinMaxDepth(mind, maxd);
|
---|
| 2611 |
|
---|
| 2612 | cout << "old tree balance: " << mind << " " << maxd << endl;
|
---|
[2094] | 2613 | mTraversalTree = new TraversalTree;
|
---|
| 2614 |
|
---|
| 2615 | ViewCellContainer viewCells;
|
---|
[2124] | 2616 | mVspTree->CollectViewCells(viewCells, false);
|
---|
[2094] | 2617 |
|
---|
[2124] | 2618 | const long startTime = GetTime();
|
---|
[2094] | 2619 |
|
---|
| 2620 | cout << "building traversal tree ... " << endl;
|
---|
| 2621 |
|
---|
[2124] | 2622 | mTraversalTree->Construct(viewCells);
|
---|
[2094] | 2623 |
|
---|
[2124] | 2624 | cout << "finished traversal tree construction in " << TimeDiff(startTime, GetTime()) * 1e-3
|
---|
[2094] | 2625 | << " secs " << endl;
|
---|
[2124] | 2626 |
|
---|
| 2627 | Debug << "*** TraversalTree Stats ***" << endl;
|
---|
| 2628 | Debug << mTraversalTree->GetStatistics() << endl;
|
---|
| 2629 |
|
---|
| 2630 | if (1)
|
---|
| 2631 | {
|
---|
| 2632 | Exporter *exporter = Exporter::GetExporter("traversal.wrl");
|
---|
| 2633 | exporter->ExportTraversalTree(*mTraversalTree, true);
|
---|
| 2634 | delete exporter;
|
---|
| 2635 | }
|
---|
[1843] | 2636 | }
|
---|
[2073] | 2637 |
|
---|
| 2638 |
|
---|
[2094] | 2639 | int HierarchyManager::CastLineSegment(const Vector3 &origin,
|
---|
| 2640 | const Vector3 &termination,
|
---|
| 2641 | ViewCellContainer &viewcells,
|
---|
| 2642 | const bool useMailboxing)
|
---|
| 2643 | {
|
---|
| 2644 | if (!mTraversalTree)
|
---|
[2124] | 2645 | {
|
---|
[2094] | 2646 | return mVspTree->CastLineSegment(origin,termination, viewcells, useMailboxing);
|
---|
[2124] | 2647 | }
|
---|
[2094] | 2648 | else
|
---|
[2124] | 2649 | {
|
---|
[2094] | 2650 | return mTraversalTree->CastLineSegment(origin,termination, viewcells, useMailboxing);
|
---|
[2124] | 2651 | }
|
---|
[2073] | 2652 | }
|
---|
[2094] | 2653 |
|
---|
| 2654 |
|
---|
| 2655 | }
|
---|