llsurfacepatch.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /**
  2. * @file llsurfacepatch.cpp
  3. * @brief LLSurfacePatch class implementation
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llsurfacepatch.h"
  34. #include "llnoise.h"
  35. #include "llappviewer.h" // For gFrameTime*
  36. #include "lldrawpool.h"
  37. #include "llpatchvertexarray.h"
  38. #include "llpipeline.h"
  39. #include "llsky.h"
  40. #include "llsurface.h"
  41. #include "llviewercamera.h"
  42. #include "llviewerobjectlist.h"
  43. #include "llviewerregion.h"
  44. #include "llvlcomposition.h"
  45. const U32 gDirAdjacent[8][2] =
  46. {
  47. { 4, 7 },
  48. { 4, 5 },
  49. { 5, 6 },
  50. { 6, 7 },
  51. { 0, 1 },
  52. { 1, 2 },
  53. { 2, 3 },
  54. { 0, 3 }
  55. };
  56. const U32 gDirOpposite[8] = { 2, 3, 0, 1, 6, 7, 4, 5 };
  57. F32 LLSurfacePatch::sNextAllowedReloadTime = 0.f;
  58. F32 LLSurfacePatch::sAutoReloadDelay = 0.f;
  59. bool LLSurfacePatch::sNeedsPatchesReload = false;
  60. LLSurfacePatch::LLSurfacePatch()
  61. : mHasReceivedData(false),
  62. mSTexUpdate(false),
  63. mDirty(false),
  64. mDirtyZStats(true),
  65. mHeightsGenerated(false),
  66. mDataOffset(0),
  67. mDataZ(NULL),
  68. mDataNorm(NULL),
  69. mVObjp(NULL),
  70. mOriginRegion(0.f, 0.f, 0.f),
  71. mCenterRegion(0.f, 0.f, 0.f),
  72. mMinZ(0.f),
  73. mMaxZ(0.f),
  74. mMeanZ(0.f),
  75. mRadius(0.f),
  76. mMinComposition(0.f),
  77. mMaxComposition(0.f),
  78. mMeanComposition(0.f),
  79. // This flag is used to communicate between adjacent surfaces and is set to
  80. // non-zero values by higher classes.
  81. mConnectedEdge(NO_EDGE),
  82. mLastUpdateTime(0),
  83. mFirstFailureTime(0.f),
  84. mSurfacep(NULL)
  85. {
  86. for (S32 i = 0; i < 8; ++i)
  87. {
  88. setNeighborPatch(i, NULL);
  89. }
  90. for (S32 i = 0; i < 9; ++i)
  91. {
  92. mNormalsInvalid[i] = true;
  93. }
  94. }
  95. LLSurfacePatch::~LLSurfacePatch()
  96. {
  97. mVObjp = NULL;
  98. }
  99. void LLSurfacePatch::dirty()
  100. {
  101. // These are outside of the loop in case we are still waiting for a dirty
  102. // from the texture being updated...
  103. if (mVObjp.notNull())
  104. {
  105. mVObjp->dirtyGeom();
  106. }
  107. else
  108. {
  109. llwarns << "No viewer object for this surface patch !" << llendl;
  110. }
  111. mDirtyZStats = true;
  112. mHeightsGenerated = false;
  113. if (!mDirty)
  114. {
  115. mDirty = true;
  116. mSurfacep->dirtySurfacePatch(this);
  117. }
  118. }
  119. void LLSurfacePatch::setSurface(LLSurface* surfacep)
  120. {
  121. mSurfacep = surfacep;
  122. LLViewerRegion* regionp = mSurfacep->getRegion();
  123. if (!regionp) // Paranoia
  124. {
  125. return;
  126. }
  127. // Surface patch object already created.
  128. if (mVObjp.notNull())
  129. {
  130. return;
  131. }
  132. llassert(mSurfacep->mType == 'l');
  133. mVObjp =
  134. (LLVOSurfacePatch*)gObjectList.createObjectViewer(LLViewerObject::LL_VO_SURFACE_PATCH,
  135. regionp);
  136. mVObjp->setPatch(this);
  137. mVObjp->setPositionRegion(mCenterRegion);
  138. gPipeline.createObject(mVObjp);
  139. }
  140. void LLSurfacePatch::disconnectNeighbor(LLSurface* surfacep)
  141. {
  142. LLSurfacePatch* neighbor;
  143. for (U32 i = 0; i < 8; ++i)
  144. {
  145. neighbor = getNeighborPatch(i);
  146. if (neighbor)
  147. {
  148. if (neighbor->mSurfacep == surfacep)
  149. {
  150. setNeighborPatch(i, NULL);
  151. mNormalsInvalid[i] = true;
  152. }
  153. }
  154. }
  155. // Clean up connected edges
  156. neighbor = getNeighborPatch(EAST);
  157. if (neighbor)
  158. {
  159. if (neighbor->mSurfacep == surfacep)
  160. {
  161. mConnectedEdge &= ~EAST_EDGE;
  162. }
  163. }
  164. neighbor = getNeighborPatch(NORTH);
  165. if (neighbor)
  166. {
  167. if (neighbor->mSurfacep == surfacep)
  168. {
  169. mConnectedEdge &= ~NORTH_EDGE;
  170. }
  171. }
  172. neighbor = getNeighborPatch(WEST);
  173. if (neighbor)
  174. {
  175. if (neighbor->mSurfacep == surfacep)
  176. {
  177. mConnectedEdge &= ~WEST_EDGE;
  178. }
  179. }
  180. neighbor = getNeighborPatch(SOUTH);
  181. if (neighbor)
  182. {
  183. if (neighbor->mSurfacep == surfacep)
  184. {
  185. mConnectedEdge &= ~SOUTH_EDGE;
  186. }
  187. }
  188. }
  189. LLVector3 LLSurfacePatch::getPointAgent(U32 x, U32 y) const
  190. {
  191. LLVector3 pos;
  192. if (mSurfacep)
  193. {
  194. U32 surface_stride = mSurfacep->getGridsPerEdge();
  195. U32 point_offset = x + y * surface_stride;
  196. pos = getOriginAgent();
  197. pos.mV[VX] += x * mSurfacep->getMetersPerGrid();
  198. pos.mV[VY] += y * mSurfacep->getMetersPerGrid();
  199. pos.mV[VZ] = *(mDataZ + point_offset);
  200. }
  201. return pos;
  202. }
  203. LLVector2 LLSurfacePatch::getTexCoords(U32 x, U32 y) const
  204. {
  205. LLVector3 rel_pos;
  206. if (mSurfacep)
  207. {
  208. U32 surface_stride = mSurfacep->getGridsPerEdge();
  209. U32 point_offset = x + y * surface_stride;
  210. LLVector3 pos = getOriginAgent();
  211. pos.mV[VX] += x * mSurfacep->getMetersPerGrid();
  212. pos.mV[VY] += y * mSurfacep->getMetersPerGrid();
  213. pos.mV[VZ] = *(mDataZ + point_offset);
  214. rel_pos = pos - mSurfacep->getOriginAgent();
  215. rel_pos *= 1.f / surface_stride;
  216. }
  217. return LLVector2(rel_pos.mV[VX], rel_pos.mV[VY]);
  218. }
  219. void LLSurfacePatch::eval(U32 x, U32 y, U32 stride, LLVector3* vertex,
  220. LLVector3* normal, LLVector2* tex0, LLVector2* tex1)
  221. {
  222. if (!mSurfacep || !mSurfacep->getGridsPerEdge() || !mVObjp)
  223. {
  224. return;
  225. }
  226. LLViewerRegion* regionp = mSurfacep->getRegion();
  227. if (!regionp)
  228. {
  229. return;
  230. }
  231. llassert_always(vertex && normal && tex0 && tex1);
  232. U32 surface_stride = mSurfacep->getGridsPerEdge();
  233. U32 point_offset = x + y * surface_stride;
  234. *normal = getNormal(x, y);
  235. LLVector3 pos_agent = getOriginAgent();
  236. pos_agent.mV[VX] += x * mSurfacep->getMetersPerGrid();
  237. pos_agent.mV[VY] += y * mSurfacep->getMetersPerGrid();
  238. pos_agent.mV[VZ] = *(mDataZ + point_offset);
  239. *vertex = pos_agent - mVObjp->getRegion()->getOriginAgent();
  240. LLVector3 rel_pos = pos_agent - mSurfacep->getOriginAgent();
  241. LLVector3 tex_pos = rel_pos * (1.f / surface_stride);
  242. tex0->mV[0] = tex_pos.mV[0];
  243. tex0->mV[1] = tex_pos.mV[1];
  244. tex1->mV[0] = regionp->getCompositionXY(llfloor(mOriginRegion.mV[0]) + x,
  245. llfloor(mOriginRegion.mV[1]) + y);
  246. constexpr F32 XYSCALEINV = 0.2222222222f / (4.9215f * 7.f);
  247. F32 vec[3] =
  248. {
  249. (F32)fmod((F32)(mOriginGlobal.mdV[0] + x) * XYSCALEINV, 256.f),
  250. (F32)fmod((F32)(mOriginGlobal.mdV[1] + y) * XYSCALEINV, 256.f),
  251. 0.f
  252. };
  253. F32 rand_val = llclamp(noise2(vec)* 0.75f + 0.5f, 0.f, 1.f);
  254. tex1->mV[1] = rand_val;
  255. }
  256. void LLSurfacePatch::calcNormal(U32 x, U32 y, U32 stride)
  257. {
  258. if (!mSurfacep)
  259. {
  260. return;
  261. }
  262. U32 patch_width = mSurfacep->mPVArray.mPatchWidth;
  263. U32 surface_stride = mSurfacep->getGridsPerEdge();
  264. const F32 mpg = mSurfacep->getMetersPerGrid() * stride;
  265. S32 poffsets[2][2][2];
  266. poffsets[0][0][0] = x - stride;
  267. poffsets[0][0][1] = y - stride;
  268. poffsets[0][1][0] = x - stride;
  269. poffsets[0][1][1] = y + stride;
  270. poffsets[1][0][0] = x + stride;
  271. poffsets[1][0][1] = y - stride;
  272. poffsets[1][1][0] = x + stride;
  273. poffsets[1][1][1] = y + stride;
  274. const LLSurfacePatch* ppatches[2][2];
  275. // LLVector3 p1, p2, p3, p4;
  276. ppatches[0][0] = this;
  277. ppatches[0][1] = this;
  278. ppatches[1][0] = this;
  279. ppatches[1][1] = this;
  280. U32 i, j;
  281. for (i = 0; i < 2; ++i)
  282. {
  283. for (j = 0; j < 2; ++j)
  284. {
  285. if (poffsets[i][j][0] < 0)
  286. {
  287. if (!ppatches[i][j]->getNeighborPatch(WEST))
  288. {
  289. poffsets[i][j][0] = 0;
  290. }
  291. else
  292. {
  293. poffsets[i][j][0] += patch_width;
  294. ppatches[i][j] = ppatches[i][j]->getNeighborPatch(WEST);
  295. }
  296. }
  297. if (poffsets[i][j][1] < 0)
  298. {
  299. if (!ppatches[i][j]->getNeighborPatch(SOUTH))
  300. {
  301. poffsets[i][j][1] = 0;
  302. }
  303. else
  304. {
  305. poffsets[i][j][1] += patch_width;
  306. ppatches[i][j] = ppatches[i][j]->getNeighborPatch(SOUTH);
  307. }
  308. }
  309. if (poffsets[i][j][0] >= (S32)patch_width)
  310. {
  311. if (!ppatches[i][j]->getNeighborPatch(EAST))
  312. {
  313. poffsets[i][j][0] = patch_width - 1;
  314. }
  315. else
  316. {
  317. poffsets[i][j][0] -= patch_width;
  318. ppatches[i][j] = ppatches[i][j]->getNeighborPatch(EAST);
  319. }
  320. }
  321. if (poffsets[i][j][1] >= (S32)patch_width)
  322. {
  323. if (!ppatches[i][j]->getNeighborPatch(NORTH))
  324. {
  325. poffsets[i][j][1] = patch_width - 1;
  326. }
  327. else
  328. {
  329. poffsets[i][j][1] -= patch_width;
  330. ppatches[i][j] = ppatches[i][j]->getNeighborPatch(NORTH);
  331. }
  332. }
  333. }
  334. }
  335. LLVector3 p00(-mpg, -mpg,
  336. *(ppatches[0][0]->mDataZ + poffsets[0][0][0] +
  337. poffsets[0][0][1] * surface_stride));
  338. LLVector3 p01(-mpg, +mpg,
  339. *(ppatches[0][1]->mDataZ + poffsets[0][1][0] +
  340. poffsets[0][1][1] * surface_stride));
  341. LLVector3 p10(+mpg, -mpg,
  342. *(ppatches[1][0]->mDataZ + poffsets[1][0][0] +
  343. poffsets[1][0][1] * surface_stride));
  344. LLVector3 p11(+mpg, +mpg,
  345. *(ppatches[1][1]->mDataZ + poffsets[1][1][0] +
  346. poffsets[1][1][1] * surface_stride));
  347. LLVector3 c1 = p11 - p00;
  348. LLVector3 c2 = p01 - p10;
  349. LLVector3 normal = c1;
  350. normal %= c2;
  351. normal.normalize();
  352. *(mDataNorm + surface_stride * y + x) = normal;
  353. }
  354. const LLVector3& LLSurfacePatch::getNormal(U32 x, U32 y) const
  355. {
  356. U32 surface_stride = mSurfacep->getGridsPerEdge();
  357. return *(mDataNorm + surface_stride * y + x);
  358. }
  359. void LLSurfacePatch::updateCameraDistanceRegion(const LLVector3& pos_region)
  360. {
  361. if (LLPipeline::sDynamicLOD)
  362. {
  363. if (!gShiftFrame)
  364. {
  365. LLVector3 dv = pos_region;
  366. dv -= mCenterRegion;
  367. mVisInfo.mDistance = llmax(0.f, (F32)(dv.length() - mRadius)) /
  368. llmax(LLVOSurfacePatch::sLODFactor, 0.1f);
  369. }
  370. }
  371. else
  372. {
  373. mVisInfo.mDistance = 0.f;
  374. }
  375. }
  376. F32 LLSurfacePatch::getDistance() const
  377. {
  378. return mVisInfo.mDistance;
  379. }
  380. // Called when a patch has changed its height field
  381. // data.
  382. void LLSurfacePatch::updateVerticalStats()
  383. {
  384. if (!mDirtyZStats || !mSurfacep || !mSurfacep->getRegion())
  385. {
  386. return;
  387. }
  388. U32 grids_per_patch_edge = mSurfacep->getGridsPerPatchEdge();
  389. U32 grids_per_edge = mSurfacep->getGridsPerEdge();
  390. F32 z = *mDataZ;
  391. mMinZ = mMaxZ = z;
  392. U32 k = 0;
  393. F32 total = 0.f;
  394. // Iterate to +1 because we need to do the edges correctly.
  395. for (U32 j = 0; j <= grids_per_patch_edge; ++j)
  396. {
  397. for (U32 i = 0; i <= grids_per_patch_edge; ++i)
  398. {
  399. z = *(mDataZ + i + j * grids_per_edge);
  400. if (z < mMinZ)
  401. {
  402. mMinZ = z;
  403. }
  404. if (z > mMaxZ)
  405. {
  406. mMaxZ = z;
  407. }
  408. total += z;
  409. ++k;
  410. }
  411. }
  412. mMeanZ = total / (F32)k;
  413. mCenterRegion.mV[VZ] = 0.5f * (mMinZ + mMaxZ);
  414. F32 meters_per_grid = mSurfacep->getMetersPerGrid();
  415. LLVector3 diam_vec(meters_per_grid * grids_per_patch_edge,
  416. meters_per_grid * grids_per_patch_edge,
  417. mMaxZ - mMinZ);
  418. mRadius = diam_vec.length() * 0.5f;
  419. mSurfacep->mMaxZ = llmax(mMaxZ, mSurfacep->mMaxZ);
  420. mSurfacep->mMinZ = llmin(mMinZ, mSurfacep->mMinZ);
  421. mSurfacep->mHasZData = true;
  422. mSurfacep->getRegion()->calculateCenterGlobal();
  423. if (mVObjp.notNull())
  424. {
  425. mVObjp->dirtyPatch();
  426. }
  427. mDirtyZStats = false;
  428. }
  429. void LLSurfacePatch::updateNormals()
  430. {
  431. if (!mSurfacep || mSurfacep->mType == 'w')
  432. {
  433. return;
  434. }
  435. U32 grids_per_patch_edge = mSurfacep->getGridsPerPatchEdge();
  436. U32 grids_per_edge = mSurfacep->getGridsPerEdge();
  437. bool dirty_patch = false;
  438. // Update the east edge
  439. if (mNormalsInvalid[EAST] || mNormalsInvalid[NORTHEAST] ||
  440. mNormalsInvalid[SOUTHEAST])
  441. {
  442. for (U32 j = 0; j <= grids_per_patch_edge; ++j)
  443. {
  444. calcNormal(grids_per_patch_edge, j, 2);
  445. calcNormal(grids_per_patch_edge - 1, j, 2);
  446. calcNormal(grids_per_patch_edge - 2, j, 2);
  447. }
  448. dirty_patch = true;
  449. }
  450. // Update the north edge
  451. if (mNormalsInvalid[NORTHEAST] || mNormalsInvalid[NORTH] ||
  452. mNormalsInvalid[NORTHWEST])
  453. {
  454. for (U32 i = 0; i <= grids_per_patch_edge; ++i)
  455. {
  456. calcNormal(i, grids_per_patch_edge, 2);
  457. calcNormal(i, grids_per_patch_edge - 1, 2);
  458. calcNormal(i, grids_per_patch_edge - 2, 2);
  459. }
  460. dirty_patch = true;
  461. }
  462. // Update the west edge
  463. if (mNormalsInvalid[NORTHWEST] || mNormalsInvalid[WEST] ||
  464. mNormalsInvalid[SOUTHWEST])
  465. {
  466. for (U32 j = 0; j < grids_per_patch_edge; ++j)
  467. {
  468. calcNormal(0, j, 2);
  469. calcNormal(1, j, 2);
  470. }
  471. dirty_patch = true;
  472. }
  473. // Update the south edge
  474. if (mNormalsInvalid[SOUTHWEST] || mNormalsInvalid[SOUTH] ||
  475. mNormalsInvalid[SOUTHEAST])
  476. {
  477. for (U32 i = 0; i < grids_per_patch_edge; ++i)
  478. {
  479. calcNormal(i, 0, 2);
  480. calcNormal(i, 1, 2);
  481. }
  482. dirty_patch = true;
  483. }
  484. // Invalidating the northeast corner is different, because depending on
  485. // what the adjacent neighbors are, we'll want to do different things.
  486. if (mNormalsInvalid[NORTHEAST])
  487. {
  488. if (!getNeighborPatch(NORTHEAST))
  489. {
  490. if (!getNeighborPatch(NORTH))
  491. {
  492. if (!getNeighborPatch(EAST))
  493. {
  494. // No north or east neighbors. Pull from the diagonal in
  495. // your own patch.
  496. *(mDataZ + grids_per_patch_edge +
  497. grids_per_patch_edge * grids_per_edge) =
  498. *(mDataZ + grids_per_patch_edge - 1 +
  499. (grids_per_patch_edge - 1) * grids_per_edge);
  500. }
  501. else if (getNeighborPatch(EAST)->getHasReceivedData())
  502. {
  503. // East, but not north. Pull from your east neighbor's
  504. // northwest point.
  505. *(mDataZ + grids_per_patch_edge +
  506. grids_per_patch_edge * grids_per_edge) =
  507. *(getNeighborPatch(EAST)->mDataZ +
  508. (grids_per_patch_edge - 1) * grids_per_edge);
  509. }
  510. else
  511. {
  512. *(mDataZ + grids_per_patch_edge +
  513. grids_per_patch_edge * grids_per_edge) =
  514. *(mDataZ + grids_per_patch_edge - 1 +
  515. (grids_per_patch_edge - 1) * grids_per_edge);
  516. }
  517. }
  518. // At this point, we know we have a north
  519. else if (getNeighborPatch(EAST))
  520. {
  521. // North and east neighbors, but not northeast.
  522. // Pull from diagonal in your own patch.
  523. *(mDataZ + grids_per_patch_edge +
  524. grids_per_patch_edge * grids_per_edge) =
  525. *(mDataZ + grids_per_patch_edge - 1 +
  526. (grids_per_patch_edge - 1) * grids_per_edge);
  527. }
  528. else if (getNeighborPatch(NORTH)->getHasReceivedData())
  529. {
  530. // North, but not east. Pull from your north neighbor's
  531. // southeast corner.
  532. *(mDataZ + grids_per_patch_edge +
  533. grids_per_patch_edge * grids_per_edge) =
  534. *(getNeighborPatch(NORTH)->mDataZ +
  535. grids_per_patch_edge - 1);
  536. }
  537. else
  538. {
  539. *(mDataZ + grids_per_patch_edge +
  540. grids_per_patch_edge * grids_per_edge) =
  541. *(mDataZ + grids_per_patch_edge - 1 +
  542. (grids_per_patch_edge - 1) * grids_per_edge);
  543. }
  544. }
  545. else if (getNeighborPatch(NORTHEAST)->mSurfacep != mSurfacep)
  546. {
  547. if ((!getNeighborPatch(NORTH) ||
  548. getNeighborPatch(NORTH)->mSurfacep != mSurfacep) &&
  549. (!getNeighborPatch(EAST) ||
  550. getNeighborPatch(EAST)->mSurfacep != mSurfacep))
  551. {
  552. *(mDataZ + grids_per_patch_edge +
  553. grids_per_patch_edge * grids_per_edge) =
  554. *(getNeighborPatch(NORTHEAST)->mDataZ);
  555. }
  556. }
  557. #if 0
  558. else
  559. {
  560. // We have got a northeast patch in the same surface. The z and
  561. // normals will be handled by that patch.
  562. }
  563. #endif
  564. calcNormal(grids_per_patch_edge, grids_per_patch_edge, 2);
  565. calcNormal(grids_per_patch_edge, grids_per_patch_edge - 1, 2);
  566. calcNormal(grids_per_patch_edge - 1, grids_per_patch_edge, 2);
  567. calcNormal(grids_per_patch_edge - 1, grids_per_patch_edge - 1, 2);
  568. dirty_patch = true;
  569. }
  570. // Update the middle normals
  571. if (mNormalsInvalid[MIDDLEMAP])
  572. {
  573. for (U32 j = 2; j < grids_per_patch_edge - 2; ++j)
  574. {
  575. for (U32 i = 2; i < grids_per_patch_edge - 2; ++i)
  576. {
  577. calcNormal(i, j, 2);
  578. }
  579. }
  580. dirty_patch = true;
  581. }
  582. if (dirty_patch)
  583. {
  584. mSurfacep->dirtySurfacePatch(this);
  585. }
  586. for (U32 i = 0; i < 9; ++i)
  587. {
  588. mNormalsInvalid[i] = false;
  589. }
  590. }
  591. void LLSurfacePatch::updateEastEdge()
  592. {
  593. U32 grids_per_patch_edge = mSurfacep->getGridsPerPatchEdge();
  594. U32 grids_per_edge = mSurfacep->getGridsPerEdge();
  595. F32* west_surface;
  596. F32* east_surface;
  597. if (!getNeighborPatch(EAST))
  598. {
  599. west_surface = mDataZ + grids_per_patch_edge;
  600. east_surface = mDataZ + grids_per_patch_edge - 1;
  601. }
  602. else if (mConnectedEdge & EAST_EDGE)
  603. {
  604. west_surface = mDataZ + grids_per_patch_edge;
  605. east_surface = getNeighborPatch(EAST)->mDataZ;
  606. }
  607. else
  608. {
  609. return;
  610. }
  611. // If patchp is on the east edge of its surface, then we update the east
  612. // side buffer
  613. for (U32 j = 0; j < grids_per_patch_edge; ++j)
  614. {
  615. U32 k = j * grids_per_edge;
  616. *(west_surface + k) = *(east_surface + k); // update buffer Z
  617. }
  618. }
  619. void LLSurfacePatch::updateNorthEdge()
  620. {
  621. U32 grids_per_patch_edge = mSurfacep->getGridsPerPatchEdge();
  622. U32 grids_per_edge = mSurfacep->getGridsPerEdge();
  623. F32* south_surface;
  624. F32* north_surface;
  625. if (!getNeighborPatch(NORTH))
  626. {
  627. south_surface = mDataZ + grids_per_patch_edge * grids_per_edge;
  628. north_surface = mDataZ + (grids_per_patch_edge - 1) * grids_per_edge;
  629. }
  630. else if (mConnectedEdge & NORTH_EDGE)
  631. {
  632. south_surface = mDataZ + grids_per_patch_edge * grids_per_edge;
  633. north_surface = getNeighborPatch(NORTH)->mDataZ;
  634. }
  635. else
  636. {
  637. return;
  638. }
  639. // Update patchp's north edge ...
  640. for (U32 i = 0; i < grids_per_patch_edge; ++i)
  641. {
  642. *(south_surface + i) = *(north_surface + i); // update buffer Z
  643. }
  644. }
  645. // Returns true when the update is considered done for this patch.
  646. bool LLSurfacePatch::updateTexture()
  647. {
  648. if (!mSTexUpdate || !mSurfacep)
  649. {
  650. return true;
  651. }
  652. LLViewerRegion* regionp = mSurfacep->getRegion();
  653. if (!regionp)
  654. {
  655. return true;
  656. }
  657. // Wait for all neighbours data to be received.
  658. LLSurfacePatch* patchp = getNeighborPatch(EAST);
  659. if (patchp && !patchp->getHasReceivedData())
  660. {
  661. return false;
  662. }
  663. patchp = getNeighborPatch(WEST);
  664. if (patchp && !patchp->getHasReceivedData())
  665. {
  666. return false;
  667. }
  668. patchp = getNeighborPatch(SOUTH);
  669. if (patchp && !patchp->getHasReceivedData())
  670. {
  671. return false;
  672. }
  673. patchp = getNeighborPatch(NORTH);
  674. if (patchp && !patchp->getHasReceivedData())
  675. {
  676. return false;
  677. }
  678. LLVLComposition* comp = regionp->getComposition();
  679. // Do check the parameters are ready now, to avoid a failed call to
  680. // LLVLComposition::generateLandTile() in LLSurfacePatch::updateGL(). HB
  681. if (!comp->getParamsReady())
  682. {
  683. return false;
  684. }
  685. if (!mHeightsGenerated)
  686. {
  687. F32 meters_per_grid = mSurfacep->getMetersPerGrid();
  688. F32 grids_per_patch_edge = mSurfacep->getGridsPerPatchEdge();
  689. F32 patch_size = meters_per_grid * (grids_per_patch_edge + 1);
  690. LLVector3d origin_region = getOriginGlobal() -
  691. mSurfacep->getOriginGlobal();
  692. F32 x = origin_region.mdV[VX];
  693. F32 y = origin_region.mdV[VY];
  694. if (!comp->generateHeights(x, y, patch_size, patch_size))
  695. {
  696. return false;
  697. }
  698. mHeightsGenerated = true;
  699. }
  700. // generateComposition() must be called periodically. HB
  701. if (!comp->generateComposition())
  702. {
  703. return false;
  704. }
  705. if (mVObjp.isNull())
  706. {
  707. return false;
  708. }
  709. mVObjp->dirtyGeom();
  710. gPipeline.markGLRebuild(mVObjp);
  711. LL_DEBUGS("MarkGLRebuild") << "Marked for GL rebuild: " << std::hex
  712. << (intptr_t)mVObjp.get() << std::dec
  713. << LL_ENDL;
  714. // If auto-reloading, we can accept a few seconds of fps rate slow down,
  715. // and keep updating the patch till it gets loaded, instead of aborting
  716. // it and getting a failed mini-map texture, like in LL's viewer and most
  717. // (all ?) other TPVs. HB
  718. return sAutoReloadDelay == 0.f;
  719. }
  720. void LLSurfacePatch::updateGL()
  721. {
  722. if (!mSurfacep) return; // Paranoia
  723. LLViewerRegion* regionp = mSurfacep->getRegion();
  724. if (!regionp) return;
  725. F32 meters_per_grid = mSurfacep->getMetersPerGrid();
  726. F32 grids_per_patch_edge = mSurfacep->getGridsPerPatchEdge();
  727. LLVector3d origin_region = getOriginGlobal() -
  728. mSurfacep->getOriginGlobal();
  729. updateCompositionStats(regionp);
  730. F32 x = origin_region.mdV[VX];
  731. F32 y = origin_region.mdV[VY];
  732. F32 size = meters_per_grid * grids_per_patch_edge;
  733. if (regionp->getComposition()->generateLandTile(x, y, size, size))
  734. {
  735. mSTexUpdate = false;
  736. mFirstFailureTime = 0.f;
  737. // Also generate the water texture
  738. mSurfacep->generateWaterTexture(x, y, size, size);
  739. return; // Success
  740. }
  741. // *HACK: register the first time we failed to generate a texture for
  742. // this patch, and if we are failing for too long, force-reload all
  743. // patches. HB
  744. if (sAutoReloadDelay > 0.f)
  745. {
  746. if (mFirstFailureTime < 0.f)
  747. {
  748. // Do not retry this hack indefinitely !
  749. return;
  750. }
  751. else if (mFirstFailureTime <= sNextAllowedReloadTime)
  752. {
  753. mFirstFailureTime = gFrameTimeSeconds;
  754. }
  755. else if (gFrameTimeSeconds - mFirstFailureTime > sAutoReloadDelay)
  756. {
  757. sNeedsPatchesReload = true;
  758. mFirstFailureTime = -1.f; // Do not retry
  759. }
  760. }
  761. }
  762. //static
  763. void LLSurfacePatch::setAutoReloadDelay(U32 delay)
  764. {
  765. if (delay)
  766. {
  767. delay = llclamp(delay, 5, 30);
  768. }
  769. sAutoReloadDelay = delay;
  770. }
  771. //static
  772. void LLSurfacePatch::allPatchesReloaded()
  773. {
  774. // Set the delay till the next possible auto-retry to minimum 30s and
  775. // maximum twice the auto-reload delay. HB
  776. sNextAllowedReloadTime = gFrameTimeSeconds +
  777. llmax(2.f * sAutoReloadDelay, 30.f);
  778. sNeedsPatchesReload = false;
  779. }
  780. void LLSurfacePatch::dirtyZ()
  781. {
  782. mSTexUpdate = true;
  783. // Invalidate all normals in this patch
  784. for (U32 i = 0; i < 9; ++i)
  785. {
  786. mNormalsInvalid[i] = true;
  787. }
  788. // Invalidate normals in this and neighboring patches
  789. for (U32 i = 0; i < 8; ++i)
  790. {
  791. LLSurfacePatch* neighbor = getNeighborPatch(i);
  792. if (neighbor)
  793. {
  794. U32 opposite = gDirOpposite[i];
  795. neighbor->mNormalsInvalid[opposite] = true;
  796. neighbor->dirty();
  797. if (i < 4)
  798. {
  799. neighbor->mNormalsInvalid[gDirAdjacent[opposite][0]] = true;
  800. neighbor->mNormalsInvalid[gDirAdjacent[opposite][1]] = true;
  801. }
  802. }
  803. }
  804. dirty();
  805. mLastUpdateTime = gFrameTime;
  806. }
  807. void LLSurfacePatch::setOriginGlobal(const LLVector3d& origin_global)
  808. {
  809. if (!mSurfacep) return; // Paranoia
  810. mOriginGlobal = origin_global;
  811. LLVector3 origin_region;
  812. origin_region.set(mOriginGlobal - mSurfacep->getOriginGlobal());
  813. mOriginRegion = origin_region;
  814. mCenterRegion.mV[VX] = origin_region.mV[VX] + 0.5f *
  815. mSurfacep->getGridsPerPatchEdge() *
  816. mSurfacep->getMetersPerGrid();
  817. mCenterRegion.mV[VY] = origin_region.mV[VY] + 0.5f *
  818. mSurfacep->getGridsPerPatchEdge() *
  819. mSurfacep->getMetersPerGrid();
  820. mVisInfo.mIsVisible = false;
  821. mVisInfo.mDistance = 512.f;
  822. mVisInfo.mRenderLevel = 0;
  823. mVisInfo.mRenderStride = mSurfacep->getGridsPerPatchEdge();
  824. }
  825. void LLSurfacePatch::connectNeighbor(LLSurfacePatch* neighbor_patchp,
  826. U32 direction)
  827. {
  828. llassert(neighbor_patchp);
  829. mNormalsInvalid[direction] = true;
  830. neighbor_patchp->mNormalsInvalid[gDirOpposite[direction]] = true;
  831. setNeighborPatch(direction, neighbor_patchp);
  832. neighbor_patchp->setNeighborPatch(gDirOpposite[direction], this);
  833. if (direction == EAST)
  834. {
  835. mConnectedEdge |= EAST_EDGE;
  836. neighbor_patchp->mConnectedEdge |= WEST_EDGE;
  837. }
  838. else if (direction == NORTH)
  839. {
  840. mConnectedEdge |= NORTH_EDGE;
  841. neighbor_patchp->mConnectedEdge |= SOUTH_EDGE;
  842. }
  843. else if (direction == WEST)
  844. {
  845. mConnectedEdge |= WEST_EDGE;
  846. neighbor_patchp->mConnectedEdge |= EAST_EDGE;
  847. }
  848. else if (direction == SOUTH)
  849. {
  850. mConnectedEdge |= SOUTH_EDGE;
  851. neighbor_patchp->mConnectedEdge |= NORTH_EDGE;
  852. }
  853. }
  854. void LLSurfacePatch::updateVisibility()
  855. {
  856. if (mVObjp.isNull())
  857. {
  858. return;
  859. }
  860. LLVector4a center;
  861. center.load3((mCenterRegion + mSurfacep->getOriginAgent()).mV);
  862. LLVector4a radius;
  863. radius.splat(mRadius);
  864. // Sphere in frustum on global coordinates
  865. if (gViewerCamera.AABBInFrustumNoFarClip(center, radius))
  866. {
  867. // We now need to calculate the render stride based on patchp's
  868. // distance from LLCamera render_stride is governed by a relation
  869. // something like this...
  870. //
  871. // delta_angle * patch.distance
  872. // render_stride <= ----------------------------------------
  873. // mMetersPerGrid
  874. //
  875. // where 'delta_angle' is the desired solid angle of the average
  876. // polygon on a patch.
  877. //
  878. // Any render_stride smaller than the RHS would be 'satisfactory'.
  879. // Smaller strides give more resolution, but efficiency suggests that
  880. // we use the largest of the render_strides that obey the relation.
  881. // Flexibility is achieved by modulating 'delta_angle' until we have an
  882. // acceptable number of triangles.
  883. U32 old_render_stride = mVisInfo.mRenderStride;
  884. // Calculate the render_stride using information in agent
  885. constexpr F32 DEFAULT_DELTA_ANGLE = 0.15f;
  886. F32 stride_per_distance = DEFAULT_DELTA_ANGLE /
  887. mSurfacep->getMetersPerGrid();
  888. U32 grids_per_patch_edge = mSurfacep->getGridsPerPatchEdge();
  889. U32 max_render_stride = lltrunc(mVisInfo.mDistance *
  890. stride_per_distance);
  891. max_render_stride = llmin(max_render_stride, 2 * grids_per_patch_edge);
  892. // We only use render_strides that are powers of two, so we use look-up
  893. // tables to figure out the render_level and corresponding
  894. // render_stride
  895. U32 new_render_level = mVisInfo.mRenderLevel =
  896. mSurfacep->getRenderLevel(max_render_stride);
  897. mVisInfo.mRenderStride = mSurfacep->getRenderStride(new_render_level);
  898. // The reason we check !mIsVisible is because non-visible patches
  899. // normals are not updated when their data is changed. When this
  900. // changes we can get rid of mIsVisible altogether.
  901. if (mVisInfo.mRenderStride != old_render_stride)
  902. {
  903. if (mVObjp.notNull())
  904. {
  905. mVObjp->dirtyGeom();
  906. LLSurfacePatch* neighbor = getNeighborPatch(WEST);
  907. if (neighbor && neighbor->mVObjp.notNull())
  908. {
  909. neighbor->mVObjp->dirtyGeom();
  910. }
  911. neighbor = getNeighborPatch(SOUTH);
  912. if (neighbor && neighbor->mVObjp.notNull())
  913. {
  914. neighbor->mVObjp->dirtyGeom();
  915. }
  916. }
  917. }
  918. mVisInfo.mIsVisible = true;
  919. }
  920. else
  921. {
  922. mVisInfo.mIsVisible = false;
  923. }
  924. }
  925. void LLSurfacePatch::updateCompositionStats(LLViewerRegion* regionp)
  926. {
  927. LLViewerLayer* vlp = regionp->getComposition();
  928. if (!vlp) return; // Paranoia
  929. LLVector3 origin = getOriginAgent() - mSurfacep->getOriginAgent();
  930. F32 mpg = mSurfacep->getMetersPerGrid();
  931. F32 x = origin.mV[VX];
  932. F32 y = origin.mV[VY];
  933. F32 multiplier = (F32)(mSurfacep->getGridsPerPatchEdge() + 1);
  934. F32 width = mpg * multiplier;
  935. F32 height = mpg * multiplier;
  936. F32 mean = 0.f;
  937. F32 min = vlp->getValueScaled(x, y);
  938. F32 max = min;
  939. U32 count = 0;
  940. for (F32 j = 0; j < height; j += mpg)
  941. {
  942. for (F32 i = 0; i < width; i += mpg)
  943. {
  944. F32 comp = vlp->getValueScaled(x + i, y + j);
  945. mean += comp;
  946. if (comp < min)
  947. {
  948. min = comp;
  949. }
  950. if (comp > max)
  951. {
  952. max = comp;
  953. }
  954. ++count;
  955. }
  956. }
  957. if (count)
  958. {
  959. mean /= count;
  960. }
  961. mMinComposition = min;
  962. mMeanComposition = mean;
  963. mMaxComposition = max;
  964. }
  965. void LLSurfacePatch::setNeighborPatch(U32 direction, LLSurfacePatch* neighborp)
  966. {
  967. mNeighborPatches[direction] = neighborp;
  968. mNormalsInvalid[direction] = true;
  969. if (direction < 4)
  970. {
  971. mNormalsInvalid[gDirAdjacent[direction][0]] = true;
  972. mNormalsInvalid[gDirAdjacent[direction][1]] = true;
  973. }
  974. }