llreflectionmapmanager.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472
  1. /**
  2. * @file llreflectionmapmanager.cpp
  3. * @brief LLReflectionMap, LLReflectionMapManager and LLHeroProbeManager
  4. * classes implementation.
  5. *
  6. * $LicenseInfo:firstyear=2022&license=viewergpl$
  7. *
  8. * Copyright (c) 2022, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #include "llviewerprecompiledheaders.h"
  34. #include "llreflectionmapmanager.h"
  35. #include "hbtracy.h"
  36. #include "llagent.h"
  37. #include "llappviewer.h"
  38. #if LL_RESET_HDRI_SKY_ON_REFLECTION_MAP_RESET
  39. # include "lldrawpoolwlsky.h" // For LLDrawPoolWLSky::resetHDRISky()
  40. #endif
  41. #include "llenvironment.h"
  42. #include "llpipeline.h"
  43. #include "llspatialpartition.h"
  44. #include "llstartup.h"
  45. #include "llviewercamera.h"
  46. #include "llviewercontrol.h"
  47. #include "llviewerdisplay.h" // For gTeleportDisplay, gCubeSnapshot
  48. #include "llviewerregion.h"
  49. #include "llviewershadermgr.h"
  50. #include "llviewerwindow.h"
  51. #include "llvovolume.h"
  52. #include "llworld.h"
  53. // Uniform names
  54. static LLStaticHashedString sDirection("direction");
  55. static LLStaticHashedString sMipLevel("mipLevel");
  56. static LLStaticHashedString sResScale("resScale");
  57. static LLStaticHashedString sRoughness("roughness");
  58. static LLStaticHashedString sSourceIdx("sourceIdx");
  59. static LLStaticHashedString sWidth("u_width");
  60. static LLStaticHashedString sZnear("znear");
  61. static LLStaticHashedString sZfar("zfar");
  62. static LLStaticHashedString sStrength("probe_strength");
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // LLReflectionMap class
  65. ///////////////////////////////////////////////////////////////////////////////
  66. LLReflectionMap::LLReflectionMap()
  67. : mCubeIndex(-1),
  68. mDistance(-1.f),
  69. mMinDepth(-1.f),
  70. mMaxDepth(-1.f),
  71. mRadius(16.f),
  72. mLastUpdateTime(0.f),
  73. mLastBindTime(0.f),
  74. mFadeIn(0.f),
  75. mProbeIndex(-1),
  76. mPriority(0),
  77. mOcclusionQuery(0),
  78. mOcclusionPendingFrames(0),
  79. mOccluded(false),
  80. mComplete(false)
  81. {
  82. mOrigin.clear();
  83. }
  84. LLReflectionMap::~LLReflectionMap()
  85. {
  86. if (mOcclusionQuery)
  87. {
  88. glDeleteQueries(1, &mOcclusionQuery);
  89. }
  90. mGroup = NULL;
  91. mViewerObject = NULL;
  92. }
  93. void LLReflectionMap::update(U32 resolution, U32 face, bool force_dynamic,
  94. F32 near_clip)
  95. {
  96. LL_TRACY_TIMER(TRC_REFLECTION_MAP);
  97. if (!gUsePBRShaders || mCubeIndex == -1 || mCubeArray.isNull())
  98. {
  99. return;
  100. }
  101. mLastUpdateTime = gFrameTimeSeconds;
  102. // Make sure we do not walk off the edge of the render target
  103. while (resolution > gPipeline.mRT->mDeferredScreen.getWidth() ||
  104. resolution > gPipeline.mRT->mDeferredScreen.getHeight())
  105. {
  106. resolution /= 2;
  107. }
  108. if (near_clip <= 0.f)
  109. {
  110. near_clip = getNearClip();
  111. }
  112. gViewerWindowp->cubeSnapshot(LLVector3(mOrigin.getF32ptr()), mCubeArray,
  113. face, near_clip,
  114. force_dynamic || getIsDynamic());
  115. }
  116. void LLReflectionMap::autoAdjustOrigin()
  117. {
  118. LL_TRACY_TIMER(TRC_REFLECTION_MAP);
  119. if (mComplete || mGroup.isNull() ||
  120. mGroup->hasState(LLViewerOctreeGroup::DEAD))
  121. {
  122. if (mViewerObject.notNull() && !mViewerObject->isDead())
  123. {
  124. mPriority = 1;
  125. mOrigin.load3(mViewerObject->getPositionAgent().mV);
  126. if (mViewerObject->getVolume())
  127. {
  128. LLVOVolume* vobjp = mViewerObject->asVolume();
  129. if (vobjp && vobjp->getReflectionProbeIsBox())
  130. {
  131. static const LLVector3 half(0.5f, 0.5f, 0.5f);
  132. mRadius = vobjp->getScale().scaledVec(half).length();
  133. return;
  134. }
  135. }
  136. mRadius = mViewerObject->getScale().mV[0] * 0.5f;
  137. }
  138. return;
  139. }
  140. LLSpatialPartition* partp = mGroup->getSpatialPartition();
  141. if (!partp || partp->mPartitionType != LLViewerRegion::PARTITION_VOLUME)
  142. {
  143. return;
  144. }
  145. mPriority = 0;
  146. if (!mGroup->getOctreeNode())
  147. {
  148. return;
  149. }
  150. // Cast a ray towards 8 corners of bounding box nudge origin towards center
  151. // of empty space
  152. const LLVector4a* bounds = mGroup->getBounds();
  153. mOrigin = bounds[0];
  154. LLVector4a size = bounds[1];
  155. LLVector4a corners[] =
  156. {
  157. { 1.f, 1.f, 1.f },
  158. { -1.f, 1.f, 1.f },
  159. { 1.f, -1.f, 1.f },
  160. { -1.f, -1.f, 1.f },
  161. { 1.f, 1.f, -1.f },
  162. { -1.f, 1.f, -1.f },
  163. { 1.f, -1.f, -1.f },
  164. { -1.f, -1.f, -1.f }
  165. };
  166. for (U32 i = 0; i < 8; ++i)
  167. {
  168. corners[i].mul(size);
  169. corners[i].add(bounds[0]);
  170. }
  171. LLVector4a extents[2];
  172. extents[0].setAdd(bounds[0], bounds[1]);
  173. extents[1].setSub(bounds[0], bounds[1]);
  174. // Prevent click-through exceptions to kick in during the calls to
  175. // lineSegmentIntersect(). HB
  176. gPickingProbe = true;
  177. LLVector4a intersection;
  178. bool hit = false;
  179. for (U32 i = 0; i < 8; ++i)
  180. {
  181. S32 face = -1;
  182. LLDrawable* drawablep =
  183. mGroup->lineSegmentIntersect(bounds[0], corners[i], false, false,
  184. &face, &intersection);
  185. if (drawablep)
  186. {
  187. hit = true;
  188. update_min_max(extents[0], extents[1], intersection);
  189. }
  190. else
  191. {
  192. update_min_max(extents[0], extents[1], corners[i]);
  193. }
  194. }
  195. gPickingProbe = false;
  196. if (hit)
  197. {
  198. mOrigin.setAdd(extents[0], extents[1]);
  199. mOrigin.mul(0.5f);
  200. }
  201. // Make sure origin is not under the ground
  202. F32* fp = mOrigin.getF32ptr();
  203. LLVector3 origin(fp);
  204. F32 height = gWorld.resolveLandHeightAgent(origin) + 2.f;
  205. fp[2] = llmax(fp[2], height);
  206. // Make sure radius encompasses all objects
  207. LLSimdScalar r2 = 0.0;
  208. for (S32 i = 0; i < 8; ++i)
  209. {
  210. LLVector4a v;
  211. v.setSub(corners[i], mOrigin);
  212. LLSimdScalar d = v.dot3(v);
  213. if (d > r2)
  214. {
  215. r2 = d;
  216. }
  217. }
  218. mRadius = llmax(sqrtf(r2.getF32()), 8.f);
  219. // Make sure near clip does not poke through ground
  220. fp[2] = llmax(fp[2], height + mRadius * 0.5f);
  221. }
  222. bool LLReflectionMap::intersects(LLReflectionMap* otherp)
  223. {
  224. LL_TRACY_TIMER(TRC_REFLECTION_MAP);
  225. LLVector4a delta;
  226. delta.setSub(otherp->mOrigin, mOrigin);
  227. F32 r = mRadius + otherp->mRadius;
  228. return delta.dot3(delta).getF32() < r * r;
  229. }
  230. F32 LLReflectionMap::getAmbiance()
  231. {
  232. LL_TRACY_TIMER(TRC_REFLECTION_MAP);
  233. F32 ret = 0.f;
  234. if (mViewerObject.notNull() && !mViewerObject->isDead() &&
  235. mViewerObject->getVolume())
  236. {
  237. LLVOVolume* vobjp = mViewerObject->asVolume();
  238. if (vobjp)
  239. {
  240. ret = vobjp->getReflectionProbeAmbiance();
  241. }
  242. }
  243. return ret;
  244. }
  245. F32 LLReflectionMap::getNearClip()
  246. {
  247. LL_TRACY_TIMER(TRC_REFLECTION_MAP);
  248. F32 ret = 1.f; // Default to 1m for automatic terrain probes
  249. if (mViewerObject.notNull() && !mViewerObject->isDead() &&
  250. mViewerObject->getVolume())
  251. {
  252. LLVOVolume* vobjp = mViewerObject->asVolume();
  253. if (vobjp)
  254. {
  255. ret = vobjp->getReflectionProbeNearClip();
  256. }
  257. }
  258. else if (mGroup.notNull())
  259. {
  260. // Default to half radius for automatic object probes
  261. ret = mRadius * 0.5f;
  262. }
  263. constexpr F32 MINIMUM_NEAR_CLIP = 0.1f;
  264. return llmax(ret, MINIMUM_NEAR_CLIP);
  265. }
  266. bool LLReflectionMap::getIsDynamic()
  267. {
  268. LL_TRACY_TIMER(TRC_REFLECTION_MAP);
  269. static LLCachedControl<U32> probe_detail(gSavedSettings,
  270. "RenderReflectionProbes");
  271. if (mViewerObject.isNull() || mViewerObject->isDead() ||
  272. !mViewerObject->getVolume() || (U32)probe_detail < STATIC_AND_DYNAMIC)
  273. {
  274. return false;
  275. }
  276. LLVOVolume* vovolp = mViewerObject->asVolume();
  277. return vovolp && vovolp->getReflectionProbeIsDynamic();
  278. }
  279. bool LLReflectionMap::getBox(LLMatrix4& box)
  280. {
  281. LL_TRACY_TIMER(TRC_REFLECTION_MAP);
  282. if (mViewerObject.isNull() || mViewerObject->isDead() ||
  283. !mViewerObject->getVolume())
  284. {
  285. return false;
  286. }
  287. LLVOVolume* vobjp = mViewerObject->asVolume();
  288. if (!vobjp || !vobjp->mDrawable || vobjp->mDrawable->isDead() ||
  289. !vobjp->getReflectionProbeIsBox())
  290. {
  291. return false;
  292. }
  293. static const LLVector3 half(0.5f, 0.5f, 0.5f);
  294. LLVector3 s = vobjp->getScale().scaledVec(half);
  295. mRadius = s.length();
  296. // Object to agent space (no scale)
  297. LLMatrix4a scale;
  298. scale.setIdentity();
  299. scale.applyScaleAffine(s);
  300. scale.transpose();
  301. // Construct object to camera space (with scale)
  302. LLMatrix4a mv = gGLModelView;
  303. LLMatrix4a rm(vobjp->mDrawable->getWorldMatrix());
  304. mv.mul(rm);
  305. mv.mul(scale);
  306. // Inverse is camera space to object unit cube
  307. mv.invert();
  308. box.set(mv.getF32ptr());
  309. return true;
  310. }
  311. bool LLReflectionMap::isRelevant()
  312. {
  313. LL_TRACY_TIMER(TRC_REFLECTION_MAP);
  314. static LLCachedControl<U32> probe_level(gSavedSettings,
  315. "RenderReflectionProbeLevel");
  316. static LLCachedControl<U32> reflections(gSavedSettings,
  317. "RenderReflectionProbes");
  318. U32 level = reflections ? probe_level : 0;
  319. if (mViewerObject.notNull() && !mViewerObject->isDead() && level > 0)
  320. {
  321. // Not an automatic probe
  322. return true;
  323. }
  324. if (level >= 3)
  325. {
  326. // All automatic probes are relevant
  327. return true;
  328. }
  329. if (level == 2)
  330. {
  331. // Terrain and water only, ignore probes that have a group
  332. return mGroup.isNull();
  333. }
  334. // No automatic probes, yes manual probes
  335. return mViewerObject.notNull() && !mViewerObject->isDead();
  336. }
  337. // Super sloppy, but we are doing an occlusion cull against a bounding cube of
  338. // a bounding sphere, pad radius so we assume if the eye is within the bounding
  339. // sphere of the bounding cube, the node is not culled.
  340. void LLReflectionMap::doOcclusion(const LLVector4a& eye)
  341. {
  342. LL_TRACY_TIMER(TRC_REFLECTION_MAP);
  343. if (LLGLSLShader::sProfileEnabled)
  344. {
  345. return;
  346. }
  347. F32 dist = mRadius * F_SQRT3 + 1.f;
  348. LLVector4a o;
  349. o.setSub(mOrigin, eye);
  350. bool do_query = false;
  351. if (o.getLength3().getF32() < dist)
  352. {
  353. // Eye is inside radius, do not attempt to occlude
  354. mOccluded = false;
  355. return;
  356. }
  357. if (mOcclusionQuery == 0)
  358. {
  359. // No query was previously issued, allocate one and issue
  360. glGenQueries(1, &mOcclusionQuery);
  361. do_query = true;
  362. }
  363. else
  364. {
  365. // Query was previously issued, check it and only issue a new query
  366. // if previous query is available
  367. GLuint result = 0;
  368. glGetQueryObjectuiv(mOcclusionQuery, GL_QUERY_RESULT_AVAILABLE, &result);
  369. if (result > 0)
  370. {
  371. do_query = true;
  372. glGetQueryObjectuiv(mOcclusionQuery, GL_QUERY_RESULT, &result);
  373. mOccluded = result == 0;
  374. mOcclusionPendingFrames = 0;
  375. }
  376. else
  377. {
  378. ++mOcclusionPendingFrames;
  379. }
  380. }
  381. if (do_query)
  382. {
  383. glBeginQuery(GL_ANY_SAMPLES_PASSED, mOcclusionQuery);
  384. LLGLSLShader* shaderp = LLGLSLShader::sCurBoundShaderPtr;
  385. shaderp->uniform3fv(LLShaderMgr::BOX_CENTER, 1, mOrigin.getF32ptr());
  386. shaderp->uniform3f(LLShaderMgr::BOX_SIZE, mRadius, mRadius, mRadius);
  387. gPipeline.mCubeVB->drawRange(LLRender::TRIANGLE_FAN, 0, 7, 8,
  388. get_box_fan_indices(&gViewerCamera,
  389. mOrigin));
  390. glEndQuery(GL_ANY_SAMPLES_PASSED);
  391. }
  392. }
  393. ///////////////////////////////////////////////////////////////////////////////
  394. // LLReflectionMapManager class
  395. ///////////////////////////////////////////////////////////////////////////////
  396. static U32 sUpdateCount = 0;
  397. // Helper function
  398. static void touch_default_probe(LLReflectionMap* probep)
  399. {
  400. LLVector3 origin = gViewerCamera.getOrigin();
  401. origin.mV[2] += 64.f;
  402. probep->mOrigin.load3(origin.mV);
  403. }
  404. LLReflectionMapManager::LLReflectionMapManager()
  405. : mUpdatingProbe(NULL),
  406. mUBO(0),
  407. mUpdatingFace(0),
  408. mReflectionProbeCount(0),
  409. mProbeResolution(128),
  410. mOldProbeResolution(0),
  411. mMaxProbeLOD(6.f),
  412. mLightScale(1.f),
  413. mResumeTime(0.f),
  414. mReset(false),
  415. mPaused(false),
  416. mRadiancePass(false),
  417. mRealtimeRadiancePass(false)
  418. {
  419. initCubeFree();
  420. }
  421. void LLReflectionMapManager::initCubeFree()
  422. {
  423. // Start at 1 because index 0 is reserved for mDefaultProbe
  424. for (U32 i = 1; i < LL_MAX_REFLECTION_PROBE_COUNT; ++i)
  425. {
  426. mCubeFree.push_back(i);
  427. }
  428. }
  429. struct CompareProbeDistance
  430. {
  431. LL_INLINE bool operator()(const LLPointer<LLReflectionMap>& lhs,
  432. const LLPointer<LLReflectionMap>& rhs)
  433. {
  434. return lhs->mDistance < rhs->mDistance;
  435. }
  436. };
  437. static F32 update_score(LLReflectionMap* probep)
  438. {
  439. return gFrameTimeSeconds - probep->mLastUpdateTime -
  440. probep->mDistance * 0.1f;
  441. }
  442. // Returns true if a is higher priority for an update than b
  443. static bool check_priority(LLReflectionMap* a, LLReflectionMap* b)
  444. {
  445. if (a->mCubeIndex == -1)
  446. {
  447. // Not a candidate for updating
  448. return false;
  449. }
  450. if (b->mCubeIndex == -1)
  451. {
  452. // b is not a candidate for updating, a is higher priority by default
  453. return true;
  454. }
  455. if (!a->mComplete && !b->mComplete)
  456. {
  457. // Neither probe is complete, use distance
  458. return a->mDistance < b->mDistance;
  459. }
  460. if (a->mComplete && b->mComplete)
  461. {
  462. // Both probes are complete, use update_score metric
  463. return update_score(a) > update_score(b);
  464. }
  465. if (sUpdateCount % 3 == 0)
  466. {
  467. // a or b is not complete;every third update, allow complete probes to
  468. // cut in line in front of non-complete probes to avoid spammy probe
  469. // generators from deadlocking scheduler (SL-20258).
  470. return !b->mComplete;
  471. }
  472. // Prioritize incomplete probe
  473. return b->mComplete;
  474. }
  475. void LLReflectionMapManager::update()
  476. {
  477. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  478. if (!LLPipeline::sReflectionProbesEnabled || gCubeSnapshot ||
  479. gTeleportDisplay || gDisconnected || !LLStartUp::isLoggedIn() ||
  480. gAppViewerp->logoutRequestSent())
  481. {
  482. llassert(!gCubeSnapshot); // Assert a snapshot is not in progress
  483. return;
  484. }
  485. if (mPaused && gFrameTimeSeconds > mResumeTime)
  486. {
  487. mPaused = false;
  488. }
  489. initReflectionMaps();
  490. llassert(mProbes[0] == mDefaultProbe);
  491. LLVector4a camera_pos;
  492. camera_pos.load3(gViewerCamera.getOrigin().mV);
  493. // Process kill list
  494. for (U32 i = 0, count = mKillList.size(); i < count; ++i)
  495. {
  496. const auto& probep = mKillList[i];
  497. prmap_vec_t::const_iterator start = mProbes.begin();
  498. prmap_vec_t::const_iterator end = mProbes.end();
  499. prmap_vec_t::const_iterator iter = std::find(start, end, probep);
  500. if (iter != end)
  501. {
  502. deleteProbe(iter - start);
  503. }
  504. }
  505. mKillList.clear();
  506. // Process create list
  507. for (U32 i = 0, count = mCreateList.size(); i < count; ++i)
  508. {
  509. LLReflectionMap* probep = mCreateList[i].get();
  510. if (probep) // Paranoia
  511. {
  512. mProbes.emplace_back(probep);
  513. }
  514. }
  515. mCreateList.clear();
  516. if (mProbes.empty())
  517. {
  518. return;
  519. }
  520. bool did_update = false;
  521. static LLCachedControl<U32> detail(gSavedSettings,
  522. "RenderReflectionProbes");
  523. static LLCachedControl<U32> probe_level(gSavedSettings,
  524. "RenderReflectionProbeLevel");
  525. U32 level = detail ? probe_level : 0;
  526. bool realtime = (U32)detail >= LLReflectionMap::REALTIME;
  527. if (mUpdatingProbe)
  528. {
  529. did_update = true;
  530. doProbeUpdate();
  531. }
  532. // Update distance to camera for all probes
  533. std::sort(mProbes.begin() + 1, mProbes.end(), CompareProbeDistance());
  534. llassert(mProbes[0] == mDefaultProbe && mProbes[0]->mCubeIndex == 0 &&
  535. mProbes[0]->mCubeArray == mTexture);
  536. // Make sure we are assigning cube slots to the closest probes
  537. // First free any cube indices for distant probes
  538. for (U32 i = mReflectionProbeCount, count = mProbes.size(); i < count; ++i)
  539. {
  540. LLReflectionMap* probep = mProbes[i].get();
  541. if (probep && probep->mCubeIndex != -1 && mUpdatingProbe != probep)
  542. {
  543. mCubeFree.push_back(probep->mCubeIndex);
  544. probep->mCubeArray = NULL;
  545. probep->mCubeIndex = -1;
  546. probep->mComplete = false;
  547. }
  548. }
  549. // Next distribute the free indices
  550. for (size_t i = 1, count = llmin(mReflectionProbeCount, mProbes.size());
  551. i < count && !mCubeFree.empty(); ++i)
  552. {
  553. // Find the closest probe that needs a cube index
  554. LLReflectionMap* probep = mProbes[i].get();
  555. if (probep && probep->mCubeIndex == -1)
  556. {
  557. S32 idx = allocateCubeIndex();
  558. if (!idx) // This should not happen
  559. {
  560. llwarns << "Could not allocate a new cube index." << llendl;
  561. llassert(false);
  562. }
  563. probep->mCubeArray = mTexture;
  564. probep->mCubeIndex = idx;
  565. }
  566. }
  567. LLReflectionMap* closest_dynamicp = NULL;
  568. LLReflectionMap* oldest_probep = NULL;
  569. LLReflectionMap* oldest_occludedp = NULL;
  570. LLVector4a d;
  571. for (size_t i = 0, count = mProbes.size(); i < count; ++i)
  572. {
  573. LLReflectionMap* probep = mProbes[i].get();
  574. if (probep->getNumRefs() == 1)
  575. {
  576. // No references held outside manager, delete this probe
  577. deleteProbe(i);
  578. --i;
  579. count = mProbes.size();
  580. continue;
  581. }
  582. if (probep != mDefaultProbe.get() &&
  583. (mPaused || !probep->isRelevant()))
  584. {
  585. // Skip irrelevant probes (or all non-default probes when paused).
  586. continue;
  587. }
  588. if (probep != mDefaultProbe.get())
  589. {
  590. LLViewerObject* objp = probep->mViewerObject.get();
  591. if (objp && !objp->isDead())
  592. {
  593. // Make sure probes track the object they are attached to.
  594. probep->mOrigin.load3(objp->getPositionAgent().mV);
  595. }
  596. d.setSub(camera_pos, probep->mOrigin);
  597. probep->mDistance = d.getLength3().getF32() - probep->mRadius;
  598. }
  599. else if (probep->mComplete)
  600. {
  601. // Make default probe have a distance of 64m for the purposes of
  602. // prioritization (if it is already been generated once).
  603. probep->mDistance = 64.f;
  604. }
  605. else
  606. {
  607. // Boost priority of default probe when it is not complete
  608. probep->mDistance = -4096.f;
  609. }
  610. if (probep->mComplete)
  611. {
  612. probep->autoAdjustOrigin();
  613. probep->mFadeIn = llmin(probep->mFadeIn + gFrameIntervalSeconds,
  614. 1.f);
  615. }
  616. if (probep->mOccluded && probep->mComplete)
  617. {
  618. if (!oldest_occludedp)
  619. {
  620. oldest_occludedp = probep;
  621. }
  622. else if (probep->mLastUpdateTime <
  623. oldest_occludedp->mLastUpdateTime)
  624. {
  625. oldest_occludedp = probep;
  626. }
  627. }
  628. else if (!did_update && i < mReflectionProbeCount &&
  629. (!oldest_probep || check_priority(probep, oldest_probep)))
  630. {
  631. oldest_probep = probep;
  632. }
  633. if (realtime && !closest_dynamicp && probep->mCubeIndex != -1 &&
  634. probep->getIsDynamic())
  635. {
  636. closest_dynamicp = probep;
  637. }
  638. }
  639. if (realtime && closest_dynamicp)
  640. {
  641. // Update the closest dynamic probe realtime; should do a full
  642. // irradiance pass on "odd" frames and a radiance pass on "even" frames
  643. closest_dynamicp->autoAdjustOrigin();
  644. // Store and override the value of "isRadiancePass"; parts of the
  645. // render pipeline rely on "isRadiancePass" to set lighting values etc.
  646. bool radiance_pass = isRadiancePass();
  647. mRadiancePass = mRealtimeRadiancePass;
  648. for (U32 i = 0; i < 6; ++i)
  649. {
  650. updateProbeFace(closest_dynamicp, i);
  651. }
  652. mRealtimeRadiancePass = !mRealtimeRadiancePass;
  653. // Restore "isRadiancePass"
  654. mRadiancePass = radiance_pass;
  655. }
  656. static LLCachedControl<U32> upd_period(gSavedSettings,
  657. "RenderDefaultProbeUpdatePeriod");
  658. F32 update_period = llclamp(U32(upd_period), 1, 30);
  659. if (gFrameTimeSeconds - mDefaultProbe->mLastUpdateTime < update_period)
  660. {
  661. if (!level)
  662. {
  663. // When probes are disabled do not update the default probe more
  664. // often than the prescribed update period.
  665. oldest_probep = NULL;
  666. }
  667. }
  668. else if (level)
  669. {
  670. // Wen probes are enabled do not update the default probe less often
  671. // than the prescribed update period.
  672. oldest_probep = mDefaultProbe.get();
  673. }
  674. // Switch to updating the next oldest probe
  675. if (!did_update && oldest_probep)
  676. {
  677. LLReflectionMap* probep = oldest_probep;
  678. llassert(probep->mCubeIndex != -1);
  679. probep->autoAdjustOrigin();
  680. ++sUpdateCount;
  681. mUpdatingProbe = probep;
  682. doProbeUpdate();
  683. }
  684. if (oldest_occludedp)
  685. {
  686. // As far as this occluded probe is concerned, an origin/radius update
  687. // is as good as a full update.
  688. oldest_occludedp->autoAdjustOrigin();
  689. oldest_occludedp->mLastUpdateTime = gFrameTimeSeconds;
  690. }
  691. }
  692. LLReflectionMap* LLReflectionMapManager::addProbe(LLSpatialGroup* groupp)
  693. {
  694. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  695. LLReflectionMap* probep = new LLReflectionMap();
  696. probep->mGroup = groupp;
  697. if (mDefaultProbe.isNull())
  698. {
  699. // Safety check to make sure default probe is always first probe added
  700. mDefaultProbe = new LLReflectionMap();
  701. mProbes.push_back(mDefaultProbe);
  702. }
  703. llassert(mProbes[0] == mDefaultProbe);
  704. if (groupp)
  705. {
  706. probep->mOrigin = groupp->getOctreeNode()->getCenter();
  707. }
  708. if (gCubeSnapshot)
  709. {
  710. // Snapshot is in progress, mProbes is being iterated over: defer
  711. // insertion until next update.
  712. mCreateList.emplace_back(probep);
  713. }
  714. else
  715. {
  716. mProbes.emplace_back(probep);
  717. }
  718. return probep;
  719. }
  720. struct CompareProbeDepth
  721. {
  722. bool operator()(const LLReflectionMap* lhs, const LLReflectionMap* rhs)
  723. {
  724. return lhs->mMinDepth < rhs->mMinDepth;
  725. }
  726. };
  727. void LLReflectionMapManager::getReflectionMaps(std::vector<LLReflectionMap*>& maps)
  728. {
  729. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  730. LLMatrix4a modelview = gGLModelView;
  731. LLVector4a oa; // Scratch space for transformed origin
  732. U32 count = 0;
  733. U32 last_idx = 0;
  734. const U32 maps_size = maps.size();
  735. for (U32 i = 0, probes = mProbes.size(); i < probes && count < maps_size;
  736. ++i)
  737. {
  738. LLReflectionMap* probep = mProbes[i].get();
  739. if (!probep) continue; // Paranoia ?
  740. // Something wants to use this probe, so let's indicate it has been
  741. // requested.
  742. probep->mLastBindTime = gFrameTimeSeconds;
  743. if (probep->mCubeIndex != -1)
  744. {
  745. if (!probep->mOccluded && probep->mComplete)
  746. {
  747. maps[count++] = probep;
  748. modelview.affineTransform(probep->mOrigin, oa);
  749. F32 radius = probep->mRadius;
  750. probep->mMinDepth = -oa.getF32ptr()[2] - radius;
  751. probep->mMaxDepth = -oa.getF32ptr()[2] + radius;
  752. }
  753. }
  754. else
  755. {
  756. probep->mProbeIndex = -1;
  757. }
  758. last_idx = i;
  759. }
  760. // Set remaining probe indices to -1
  761. for (U32 i = last_idx + 1, n = mProbes.size(); i < n; ++i)
  762. {
  763. LLReflectionMap* probep = mProbes[i].get();
  764. if (probep) // Paranoia ?
  765. {
  766. probep->mProbeIndex = -1;
  767. }
  768. }
  769. if (count > 1)
  770. {
  771. std::sort(maps.begin(), maps.begin() + count, CompareProbeDepth());
  772. }
  773. for (U32 i = 0; i < count; ++i)
  774. {
  775. maps[i]->mProbeIndex = i;
  776. }
  777. // NULL-terminate list
  778. if (count < maps_size)
  779. {
  780. maps[count] = NULL;
  781. }
  782. }
  783. LLReflectionMap* LLReflectionMapManager::registerSpatialGroup(LLSpatialGroup* groupp)
  784. {
  785. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  786. if (!groupp)
  787. {
  788. return NULL;
  789. }
  790. LLSpatialPartition* partp = groupp->getSpatialPartition();
  791. if (!partp || partp->mPartitionType != LLViewerRegion::PARTITION_VOLUME)
  792. {
  793. return NULL;
  794. }
  795. OctreeNode* nodep = groupp->getOctreeNode();
  796. F32 size = nodep->getSize().getF32ptr()[0];
  797. if (size < 15.f || size > 17.f)
  798. {
  799. return NULL;
  800. }
  801. return addProbe(groupp);
  802. }
  803. LLReflectionMap* LLReflectionMapManager::registerViewerObject(LLViewerObject* vobjp)
  804. {
  805. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  806. if (!vobjp || vobjp->isDead())
  807. {
  808. return NULL;
  809. }
  810. LLReflectionMap* probep = new LLReflectionMap();
  811. probep->mViewerObject = vobjp;
  812. probep->mOrigin.load3(vobjp->getPositionAgent().mV);
  813. if (gCubeSnapshot)
  814. {
  815. // Snapshot is in progress, mProbes is being iterated over, defer
  816. // insertion until next update
  817. mCreateList.emplace_back(probep);
  818. }
  819. else
  820. {
  821. mProbes.emplace_back(probep);
  822. }
  823. return probep;
  824. }
  825. S32 LLReflectionMapManager::allocateCubeIndex()
  826. {
  827. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  828. if (mCubeFree.empty())
  829. {
  830. return -1;
  831. }
  832. S32 ret = mCubeFree.front();
  833. mCubeFree.pop_front();
  834. return ret;
  835. }
  836. void LLReflectionMapManager::deleteProbe(U32 i)
  837. {
  838. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  839. LLReflectionMap* probep = mProbes[i].get();
  840. if (probep == mDefaultProbe.get())
  841. {
  842. llwarns << "Attempt to remove the default probe. Aborted." << llendl;
  843. return;
  844. }
  845. if (probep->mCubeIndex != -1)
  846. {
  847. // Mark the cube index used by this probe as being free
  848. mCubeFree.push_back(probep->mCubeIndex);
  849. }
  850. if (mUpdatingProbe == probep)
  851. {
  852. mUpdatingProbe = NULL;
  853. mUpdatingFace = 0;
  854. }
  855. // Remove from any neighbors lists
  856. for (auto& otherp : probep->mNeighbors)
  857. {
  858. LLReflectionMap::reflmap_vec_t::iterator ne = otherp->mNeighbors.end();
  859. LLReflectionMap::reflmap_vec_t::iterator it =
  860. std::find(otherp->mNeighbors.begin(), ne, probep);
  861. if (it != ne)
  862. {
  863. otherp->mNeighbors.erase(it);
  864. }
  865. }
  866. mProbes.erase(mProbes.begin() + i);
  867. }
  868. void LLReflectionMapManager::doProbeUpdate()
  869. {
  870. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  871. if (!gUsePBRShaders)
  872. {
  873. return;
  874. }
  875. llassert(mUpdatingProbe != NULL);
  876. updateProbeFace(mUpdatingProbe, mUpdatingFace);
  877. if (++mUpdatingFace == 6)
  878. {
  879. updateNeighbors(mUpdatingProbe);
  880. mUpdatingFace = 0;
  881. if (isRadiancePass())
  882. {
  883. mUpdatingProbe->mComplete = true;
  884. mUpdatingProbe = NULL;
  885. mRadiancePass = false;
  886. }
  887. else
  888. {
  889. mRadiancePass = true;
  890. }
  891. }
  892. }
  893. // Do the reflection map update render passes. For every 12 calls to this
  894. // method, one complete reflection probe radiance map and irradiance map is
  895. // generated. First six passes render the scene with direct lighting only into
  896. // a scratch space cube map at the end of the cube map array and generate a
  897. // simple mip chain (not convolution filter). At the end of these passes, an
  898. // irradiance map is generated for this probe and placed into the irradiance
  899. // cube map array at the index for this probe. The next six passes render the
  900. // scene with both radiance and irradiance into the same scratch space cube map
  901. // and generate a simple mip chain. At the end of these passes, a radiance map
  902. // is generated for this probe and placed into the radiance cube map array at
  903. // the index for this probe. In effect this simulates single-bounce lighting.
  904. void LLReflectionMapManager::updateProbeFace(LLReflectionMap* probep, U32 face)
  905. {
  906. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  907. if (!gUsePBRShaders)
  908. {
  909. return;
  910. }
  911. mLightScale = 1.f;
  912. static LLCachedControl<F32> max_amb(gSavedSettings,
  913. "RenderReflectionProbeMaxAmbiance");
  914. if (!isRadiancePass() && probep->getAmbiance() > (F32)max_amb)
  915. {
  916. mLightScale = max_amb / probep->getAmbiance();
  917. }
  918. // Hacky hot-swap of camera specific render targets
  919. auto saved_rt = gPipeline.mRT;
  920. gPipeline.mRT = &gPipeline.mAuxillaryRT;
  921. if (probep == mDefaultProbe.get())
  922. {
  923. touch_default_probe(probep);
  924. gPipeline.pushRenderTypeMask();
  925. // Only render sky, water, terrain, and clouds
  926. gPipeline.andRenderTypeMask(LLPipeline::RENDER_TYPE_SKY,
  927. LLPipeline::RENDER_TYPE_WL_SKY,
  928. LLPipeline::RENDER_TYPE_WATER,
  929. LLPipeline::RENDER_TYPE_VOIDWATER,
  930. LLPipeline::RENDER_TYPE_CLOUDS,
  931. LLPipeline::RENDER_TYPE_TERRAIN,
  932. LLPipeline::END_RENDER_TYPES);
  933. probep->update(mRenderTarget.getWidth(), face);
  934. gPipeline.popRenderTypeMask();
  935. }
  936. else
  937. {
  938. probep->update(mRenderTarget.getWidth(), face);
  939. }
  940. gPipeline.mRT = saved_rt;
  941. S32 source_idx = mReflectionProbeCount;
  942. if (probep != mUpdatingProbe)
  943. {
  944. // This is the "realtime" probe that is updating every frame, use the
  945. // secondary scratch space channel
  946. ++source_idx;
  947. }
  948. gGL.setColorMask(true, true);
  949. LLGLDepthTest depth(GL_FALSE, GL_FALSE);
  950. LLGLDisable cull(GL_CULL_FACE);
  951. LLGLDisable blend(GL_BLEND);
  952. // Downsample to placeholder map
  953. gGL.matrixMode(gGL.MM_MODELVIEW);
  954. gGL.pushMatrix();
  955. gGL.loadIdentity();
  956. gGL.matrixMode(gGL.MM_PROJECTION);
  957. gGL.pushMatrix();
  958. gGL.loadIdentity();
  959. gGL.flush();
  960. U32 res = mProbeResolution * 2;
  961. LLRenderTarget* screen_rt = &gPipeline.mAuxillaryRT.mScreen;
  962. // Perform a gaussian blur on the super sampled render before downsampling
  963. LLGLSLShader* shaderp = &gGaussianProgram;
  964. shaderp->bind();
  965. const F32 res_scale = 1.f / F32(mProbeResolution * 2);
  966. shaderp->uniform1f(sResScale, res_scale);
  967. S32 chan = shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  968. LLTexUnit::TT_TEXTURE);
  969. // Horizontal
  970. shaderp->uniform2f(sDirection, 1.f, 0.f);
  971. gGL.getTexUnit(chan)->bind(screen_rt);
  972. mRenderTarget.bindTarget();
  973. gPipeline.mScreenTriangleVB->setBuffer();
  974. gPipeline.mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  975. mRenderTarget.flush();
  976. // Vertical
  977. shaderp->uniform2f(sDirection, 0.f, 1.f);
  978. gGL.getTexUnit(chan)->bind(&mRenderTarget);
  979. screen_rt->bindTarget();
  980. gPipeline.mScreenTriangleVB->setBuffer();
  981. gPipeline.mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  982. screen_rt->flush();
  983. S32 mips = S32(log2f((F32)mProbeResolution) + 0.5f);
  984. shaderp = &gReflectionMipProgram;
  985. shaderp->bind();
  986. chan = shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  987. LLTexUnit::TT_TEXTURE);
  988. for (S32 i = 0, count = mMipChain.size(); i < count; ++i)
  989. {
  990. LLRenderTarget& target = mMipChain[i];
  991. target.bindTarget();
  992. if (i == 0)
  993. {
  994. gGL.getTexUnit(chan)->bind(screen_rt);
  995. }
  996. else
  997. {
  998. gGL.getTexUnit(chan)->bind(&(mMipChain[i - 1]));
  999. }
  1000. shaderp->uniform1f(sResScale, res_scale);
  1001. gPipeline.mScreenTriangleVB->setBuffer();
  1002. gPipeline.mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  1003. res /= 2;
  1004. S32 mip = i + mips - count;
  1005. if (mip >= 0)
  1006. {
  1007. mTexture->bind(0);
  1008. glCopyTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, mip, 0, 0,
  1009. source_idx * 6 + face, 0, 0, res, res);
  1010. mTexture->unbind();
  1011. }
  1012. target.flush();
  1013. }
  1014. gGL.popMatrix();
  1015. gGL.matrixMode(gGL.MM_MODELVIEW);
  1016. gGL.popMatrix();
  1017. gGL.getTexUnit(chan)->unbind(LLTexUnit::TT_TEXTURE);
  1018. shaderp->unbind();
  1019. if (face != 5)
  1020. {
  1021. return; // We are done.
  1022. }
  1023. if (mMipChain.empty()) // Paranoia ?
  1024. {
  1025. llwarns_once << "mMipChain is empty !" << llendl;
  1026. return;
  1027. }
  1028. if (!LLViewerShaderMgr::sHasIrrandiance)
  1029. {
  1030. // Cannot render this since the two gIrradianceGenProgram and
  1031. // gRadianceGenProgram shaders have not loaded... HB
  1032. return;
  1033. }
  1034. mMipChain[0].bindTarget();
  1035. if (isRadiancePass())
  1036. {
  1037. // Generate radiance map (even if this is not the irradiance map, we
  1038. // need the mip chain for the irradiance map).
  1039. shaderp = &gRadianceGenProgram;
  1040. shaderp->bind();
  1041. mVertexBuffer->setBuffer();
  1042. chan = shaderp->enableTexture(LLShaderMgr::REFLECTION_PROBES,
  1043. LLTexUnit::TT_CUBE_MAP_ARRAY);
  1044. mTexture->bind(chan);
  1045. shaderp->uniform1i(sSourceIdx, source_idx);
  1046. shaderp->uniform1f(LLShaderMgr::REFLECTION_PROBE_MAX_LOD,
  1047. mMaxProbeLOD);
  1048. shaderp->uniform1f(LLShaderMgr::REFLECTION_PROBE_STRENGTH, 1.f);
  1049. U32 res = mMipChain[0].getWidth();
  1050. LLCoordFrame frame;
  1051. F32 mat[16];
  1052. for (size_t i = 0, count = mMipChain.size(); i < count; ++i)
  1053. {
  1054. shaderp->uniform1f(sRoughness, F32(i) / F32(count - 1));
  1055. shaderp->uniform1f(sMipLevel, i);
  1056. shaderp->uniform1i(sWidth, mProbeResolution);
  1057. for (U32 cf = 0; cf < 6; ++cf) // For each cube face
  1058. {
  1059. frame.lookAt(LLVector3::zero,
  1060. LLCubeMapArray::sClipToCubeLookVecs[cf],
  1061. LLCubeMapArray::sClipToCubeUpVecs[cf]);
  1062. frame.getOpenGLRotation(mat);
  1063. gGL.loadMatrix(mat);
  1064. mVertexBuffer->drawArrays(gGL.TRIANGLE_STRIP, 0, 4);
  1065. glCopyTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, i, 0, 0,
  1066. probep->mCubeIndex * 6 + cf, 0, 0,
  1067. res, res);
  1068. }
  1069. if (i != count - 1)
  1070. {
  1071. res /= 2;
  1072. glViewport(0, 0, res, res);
  1073. }
  1074. }
  1075. }
  1076. else
  1077. {
  1078. // Generate irradiance map
  1079. shaderp = &gIrradianceGenProgram;
  1080. shaderp->bind();
  1081. chan = shaderp->enableTexture(LLShaderMgr::REFLECTION_PROBES,
  1082. LLTexUnit::TT_CUBE_MAP_ARRAY);
  1083. mTexture->bind(chan);
  1084. shaderp->uniform1i(sSourceIdx, source_idx);
  1085. shaderp->uniform1f(LLShaderMgr::REFLECTION_PROBE_MAX_LOD,
  1086. mMaxProbeLOD);
  1087. mVertexBuffer->setBuffer();
  1088. // Find the mip target to start with based on irradiance map resolution
  1089. U32 start_mip = 0;
  1090. U32 count = mMipChain.size();
  1091. while (start_mip < count &&
  1092. mMipChain[start_mip].getWidth() != LL_IRRADIANCE_MAP_RESOLUTION)
  1093. {
  1094. ++start_mip;
  1095. }
  1096. if (start_mip < count)
  1097. {
  1098. LLRenderTarget& target = mMipChain[start_mip];
  1099. glViewport(0, 0, target.getWidth(), target.getHeight());
  1100. F32 mat[16];
  1101. for (U32 cf = 0; cf < 6; ++cf) // For each cube face
  1102. {
  1103. LLCoordFrame frame;
  1104. frame.lookAt(LLVector3::zero,
  1105. LLCubeMapArray::sClipToCubeLookVecs[cf],
  1106. LLCubeMapArray::sClipToCubeUpVecs[cf]);
  1107. frame.getOpenGLRotation(mat);
  1108. gGL.loadMatrix(mat);
  1109. mVertexBuffer->drawArrays(gGL.TRIANGLE_STRIP, 0, 4);
  1110. S32 res = target.getWidth();
  1111. mIrradianceMaps->bind(chan);
  1112. glCopyTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, 0, 0,
  1113. probep->mCubeIndex * 6 + cf, 0, 0,
  1114. res, res);
  1115. mTexture->bind(chan);
  1116. }
  1117. }
  1118. }
  1119. mMipChain[0].flush();
  1120. shaderp->unbind();
  1121. }
  1122. void LLReflectionMapManager::pause(F32 duration)
  1123. {
  1124. mPaused = true;
  1125. mResumeTime = gFrameTimeSeconds + duration;
  1126. }
  1127. void LLReflectionMapManager::shift(const LLVector4a& offset)
  1128. {
  1129. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  1130. for (U32 i = 0, count = mProbes.size(); i < count; ++i)
  1131. {
  1132. LLReflectionMap* probep = mProbes[i].get();
  1133. if (probep) // Paranoia
  1134. {
  1135. probep->mOrigin.add(offset);
  1136. }
  1137. }
  1138. }
  1139. void LLReflectionMapManager::updateNeighbors(LLReflectionMap* probep)
  1140. {
  1141. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  1142. if (mDefaultProbe.get() == probep)
  1143. {
  1144. return;
  1145. }
  1146. // Remove from existing neighbors
  1147. for (auto& otherp : probep->mNeighbors)
  1148. {
  1149. LLReflectionMap::reflmap_vec_t::iterator ne = otherp->mNeighbors.end();
  1150. LLReflectionMap::reflmap_vec_t::iterator it =
  1151. std::find(otherp->mNeighbors.begin(), ne, probep);
  1152. if (it != ne)
  1153. {
  1154. otherp->mNeighbors.erase(it);
  1155. }
  1156. }
  1157. probep->mNeighbors.clear();
  1158. // Search for new neighbors
  1159. if (probep->isRelevant())
  1160. {
  1161. for (U32 i = 0, count = mProbes.size(); i < count; ++i)
  1162. {
  1163. LLReflectionMap* otherp = mProbes[i].get();
  1164. if (otherp != mDefaultProbe.get() && otherp != probep)
  1165. {
  1166. if (otherp->isRelevant() && probep->intersects(otherp))
  1167. {
  1168. probep->mNeighbors.push_back(otherp);
  1169. otherp->mNeighbors.push_back(probep);
  1170. }
  1171. }
  1172. }
  1173. }
  1174. }
  1175. // Structure for packing uniform buffer object.
  1176. // See class3/deferred/reflectionProbeF.glsl
  1177. struct ReflectionProbeData
  1178. {
  1179. // For box probes, matrix that transforms from camera space to a [-1, 1]
  1180. // cube representing the bounding box of the box probe
  1181. LLMatrix4 refBox[LL_MAX_REFLECTION_PROBE_COUNT];
  1182. LLMatrix4 heroBox;
  1183. // For sphere probes, origin (xyz) and radius (w) of refmaps in clip space
  1184. LLVector4 refSphere[LL_MAX_REFLECTION_PROBE_COUNT];
  1185. // Extra parameters
  1186. // x - irradiance scale
  1187. // y - radiance scale
  1188. // z - fade in
  1189. // w - znear
  1190. LLVector4 refParams[LL_MAX_REFLECTION_PROBE_COUNT];
  1191. LLVector4 heroSphere;
  1192. // Indices used by probe:
  1193. // [i][0] - cubemap array index for this probe
  1194. // [i][1] - index into "refNeighbor" for probes that intersect this probe
  1195. // [i][2] - number of probes that intersect this probe, or -1 for no
  1196. // neighbors
  1197. // [i][3] - priority (probe type stored in sign bit - positive for
  1198. // spheres, negative for boxes)
  1199. GLint refIndex[LL_MAX_REFLECTION_PROBE_COUNT][4];
  1200. // List of neighbor indices
  1201. GLint refNeighbor[4096];
  1202. // Lookup table for which index to start with for the given Z depth
  1203. GLint refBucket[256][4];
  1204. // Numbrer of active refmaps
  1205. GLint refmapCount;
  1206. GLint heroShape;
  1207. GLint heroMipCount;
  1208. GLint heroProbeCount;
  1209. };
  1210. void LLReflectionMapManager::updateUniforms()
  1211. {
  1212. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  1213. if (!LLPipeline::sReflectionProbesEnabled)
  1214. {
  1215. return;
  1216. }
  1217. mReflectionMaps.resize(mReflectionProbeCount);
  1218. getReflectionMaps(mReflectionMaps);
  1219. ReflectionProbeData rpd;
  1220. static F32 min_depth[256];
  1221. for (U32 i = 0; i < 256; ++i)
  1222. {
  1223. rpd.refBucket[i][0] = rpd.refBucket[i][1] = rpd.refBucket[i][2] =
  1224. rpd.refBucket[i][3] = mReflectionProbeCount;
  1225. min_depth[i] = FLT_MAX;
  1226. }
  1227. LLMatrix4a modelview = gGLModelView;
  1228. LLVector4a oa; // Scratch space for transformed origin
  1229. S32 count = 0;
  1230. // Neighbor "cursor": index into refNeighbor to start writing the next
  1231. // probe's list of neighbors
  1232. U32 nc = 0;
  1233. static LLCachedControl<bool> auto_adjust(gSavedSettings,
  1234. "RenderSkyAutoAdjustLegacy");
  1235. LLSettingsSky::ptr_t skyp = gEnvironment.getCurrentSky();
  1236. F32 min_ambiance = skyp->getReflectionProbeAmbiance(auto_adjust);
  1237. F32 ambscale, radscale;
  1238. if (gCubeSnapshot && !isRadiancePass()) // Ambiance pass ?
  1239. {
  1240. ambscale = 0.f;
  1241. radscale = 0.5f;
  1242. }
  1243. else
  1244. {
  1245. ambscale = radscale = 1.f;
  1246. }
  1247. for (U32 k = 0, nmaps = mReflectionMaps.size(); k < nmaps; ++k)
  1248. {
  1249. LLReflectionMap* refmapp = mReflectionMaps[k];
  1250. if (!refmapp)
  1251. {
  1252. break;
  1253. }
  1254. if (refmapp != mDefaultProbe.get())
  1255. {
  1256. // Bucket search data. Theory of operation:
  1257. // 1. Determine minimum and maximum depth of each influence volume
  1258. // and store in mDepth (done in getReflectionMaps).
  1259. // 2. Sort by minimum depth.
  1260. // 3. Prepare a bucket for each 1m of depth out to 256m.
  1261. // 4. For each bucket, store the index of the nearest probe that
  1262. // might influence pixels in that bucket.
  1263. // 5. In the shader, lookup the bucket for the pixel depth to get
  1264. // the index of the first probe that could possibly influence
  1265. // the current pixel.
  1266. U32 depth_min = U32(llclamp(S32(refmapp->mMinDepth), 0, 255));
  1267. U32 depth_max = U32(llclamp(S32(refmapp->mMaxDepth), 0, 255));
  1268. for (U32 i = depth_min; i <= depth_max; ++i)
  1269. {
  1270. if (refmapp->mMinDepth < min_depth[i])
  1271. {
  1272. min_depth[i] = refmapp->mMinDepth;
  1273. rpd.refBucket[i][0] = refmapp->mProbeIndex;
  1274. }
  1275. }
  1276. }
  1277. llassert(refmapp->mProbeIndex == count && refmapp->mCubeIndex >= 0 &&
  1278. mReflectionMaps[refmapp->mProbeIndex] == refmapp);
  1279. LLViewerObject* objp = refmapp->mViewerObject.get();
  1280. if (objp && !objp->isDead() && objp->getVolume())
  1281. {
  1282. // Have active manual probes live-track the object they are
  1283. // associated with
  1284. refmapp->mOrigin.load3(objp->getPositionAgent().mV);
  1285. LLVOVolume* vobjp = objp->asVolume();
  1286. if (vobjp && vobjp->getReflectionProbeIsBox())
  1287. {
  1288. static const LLVector3 half(0.5f, 0.5f, 0.5f);
  1289. refmapp->mRadius = vobjp->getScale().scaledVec(half).length();
  1290. }
  1291. else
  1292. {
  1293. refmapp->mRadius = objp->getScale().mV[0] * 0.5f;
  1294. }
  1295. }
  1296. modelview.affineTransform(refmapp->mOrigin, oa);
  1297. rpd.refSphere[count].set(oa.getF32ptr());
  1298. rpd.refSphere[count].mV[3] = refmapp->mRadius;
  1299. rpd.refIndex[count][0] = refmapp->mCubeIndex;
  1300. llassert(nc % 4 == 0);
  1301. rpd.refIndex[count][1] = nc / 4;
  1302. rpd.refIndex[count][3] = refmapp->mPriority;
  1303. // For objects that are reflection probes, use the volume as the
  1304. // influence volume of the probe only possibile influence volumes are
  1305. // boxes and spheres, so detect boxes and treat everything else as
  1306. // spheres
  1307. if (refmapp->getBox(rpd.refBox[count]))
  1308. {
  1309. // Negate priority to indicate this probe has a box influence
  1310. // volume
  1311. rpd.refIndex[count][3] *= -1;
  1312. }
  1313. rpd.refParams[count].set(llmax(min_ambiance,
  1314. refmapp->getAmbiance()) * ambscale,
  1315. radscale, // Radiance scale
  1316. refmapp->mFadeIn, // Fade-in weight
  1317. // Z near
  1318. oa.getF32ptr()[2] - refmapp->mRadius);
  1319. // Neighbor ("index"): index into refNeighbor to write indices for
  1320. // current reflection probe's neighbors
  1321. U32 ni = nc;
  1322. // Pack neghbor list
  1323. constexpr U32 MAX_NEIGHBORS = 64;
  1324. U32 neighbor_count = 0;
  1325. for (U32 n = 0, ncount = refmapp->mNeighbors.size();
  1326. n < ncount && ni < 4096 && neighbor_count < MAX_NEIGHBORS; ++n)
  1327. {
  1328. LLReflectionMap* neighborp = refmapp->mNeighbors[n];
  1329. GLint idx = neighborp->mProbeIndex;
  1330. if (idx != -1 && !neighborp->mOccluded &&
  1331. neighborp->mCubeIndex != -1)
  1332. {
  1333. // This neighbor may be sampled
  1334. rpd.refNeighbor[ni++] = idx;
  1335. ++neighbor_count;
  1336. }
  1337. }
  1338. if (nc == ni)
  1339. {
  1340. // No neighbors, tag as empty
  1341. rpd.refIndex[count][1] = -1;
  1342. }
  1343. else
  1344. {
  1345. rpd.refIndex[count][2] = ni - nc;
  1346. // Move the cursor forward
  1347. nc = ni;
  1348. if (nc % 4 != 0)
  1349. {
  1350. // Jump to next power of 4 for compatibility with ivec4
  1351. nc += 4 - (nc % 4);
  1352. }
  1353. }
  1354. ++count;
  1355. }
  1356. rpd.refmapCount = count;
  1357. LLHeroProbeManager& hero = gPipeline.mHeroProbeManager;
  1358. hero.updateUniforms();
  1359. // Get the hero probe data
  1360. rpd.heroBox = hero.mHeroData.heroBox;
  1361. rpd.heroSphere = hero.mHeroData.heroSphere;
  1362. rpd.heroShape = hero.mHeroData.heroShape;
  1363. rpd.heroMipCount = hero.mHeroData.heroMipCount;
  1364. rpd.heroProbeCount = hero.mHeroData.heroProbeCount;
  1365. // Copy rpd into uniform buffer object
  1366. if (mUBO == 0)
  1367. {
  1368. glGenBuffers(1, &mUBO);
  1369. }
  1370. glBindBuffer(GL_UNIFORM_BUFFER, mUBO);
  1371. glBufferData(GL_UNIFORM_BUFFER, sizeof(ReflectionProbeData), &rpd,
  1372. GL_STREAM_DRAW);
  1373. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1374. }
  1375. void LLReflectionMapManager::setUniforms()
  1376. {
  1377. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  1378. if (LLPipeline::sReflectionProbesEnabled)
  1379. {
  1380. if (mUBO == 0)
  1381. {
  1382. updateUniforms();
  1383. }
  1384. glBindBufferBase(GL_UNIFORM_BUFFER, 1, mUBO);
  1385. }
  1386. }
  1387. static void render_reflection_probe(LLReflectionMap* probep)
  1388. {
  1389. if (!probep || !probep->isRelevant())
  1390. {
  1391. return;
  1392. }
  1393. F32* po = probep->mOrigin.getF32ptr();
  1394. // Draw orange line from probe to neighbors
  1395. gGL.flush();
  1396. gGL.diffuseColor4f(1.f, 0.5f, 0.f, 1.f);
  1397. gGL.begin(gGL.LINES);
  1398. for (U32 i = 0, count = probep->mNeighbors.size(); i < count; ++i)
  1399. {
  1400. LLReflectionMap* neighborp = probep->mNeighbors[i];
  1401. if (!neighborp) continue; // Paranoia ?
  1402. if (probep->mViewerObject.isNull() ||
  1403. neighborp->mViewerObject.isNull())
  1404. {
  1405. gGL.vertex3fv(po);
  1406. gGL.vertex3fv(neighborp->mOrigin.getF32ptr());
  1407. }
  1408. }
  1409. gGL.end(true);
  1410. gGL.diffuseColor4f(1.f, 1.f, 0.f, 1.f);
  1411. gGL.begin(gGL.LINES);
  1412. for (U32 i = 0, count = probep->mNeighbors.size(); i < count; ++i)
  1413. {
  1414. LLReflectionMap* neighborp = probep->mNeighbors[i];
  1415. if (!neighborp) continue; // Paranoia ?
  1416. if (probep->mViewerObject.notNull() &&
  1417. neighborp->mViewerObject.notNull())
  1418. {
  1419. gGL.vertex3fv(po);
  1420. gGL.vertex3fv(neighborp->mOrigin.getF32ptr());
  1421. }
  1422. }
  1423. gGL.end(true);
  1424. }
  1425. void LLReflectionMapManager::renderDebug()
  1426. {
  1427. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  1428. gDebugProgram.bind();
  1429. for (size_t i = 0, count = mProbes.size(); i < count; ++i)
  1430. {
  1431. render_reflection_probe(mProbes[i].get());
  1432. }
  1433. gDebugProgram.unbind();
  1434. }
  1435. void LLReflectionMapManager::initReflectionMaps()
  1436. {
  1437. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  1438. if (!gUsePBRShaders)
  1439. {
  1440. return;
  1441. }
  1442. if (mReset || mTexture.isNull() ||
  1443. mReflectionProbeCount != LL_MAX_REFLECTION_PROBE_COUNT)
  1444. {
  1445. mReset = false;
  1446. #if LL_RESET_HDRI_SKY_ON_REFLECTION_MAP_RESET
  1447. LLDrawPoolWLSky::resetHDRISky();
  1448. #endif
  1449. static LLCachedControl<U32> res(gSavedSettings,
  1450. "RenderReflectionProbeResolution");
  1451. mProbeResolution = nhpo2(llclamp((U32)res, 64, 512));
  1452. mReflectionProbeCount = LL_MAX_REFLECTION_PROBE_COUNT;
  1453. mMaxProbeLOD = log2f(mProbeResolution) - 1.f; // Number of mips - 1
  1454. if (mTexture.isNull() ||
  1455. mTexture->getResolution() != mProbeResolution ||
  1456. mTexture->getCount() != mReflectionProbeCount + 2)
  1457. {
  1458. mTexture = new LLCubeMapArray();
  1459. // Store mReflectionProbeCount + 2 cube maps, final two cube maps
  1460. // are used for render target and radiance map generation source).
  1461. mTexture->allocate(mProbeResolution, 3, mReflectionProbeCount + 2);
  1462. mIrradianceMaps = new LLCubeMapArray();
  1463. mIrradianceMaps->allocate(LL_IRRADIANCE_MAP_RESOLUTION, 3,
  1464. mReflectionProbeCount, false);
  1465. }
  1466. // Reset probe state
  1467. mUpdatingFace = 0;
  1468. mUpdatingProbe = NULL;
  1469. mRadiancePass = mRealtimeRadiancePass = false;
  1470. // If default probe already exists, remember whether or not it is
  1471. // complete (SL-20498)
  1472. bool default_complete = mDefaultProbe.notNull() &&
  1473. mDefaultProbe->mComplete;
  1474. for (U32 i = 0, count = mProbes.size(); i < count; ++i)
  1475. {
  1476. LLReflectionMap* probep = mProbes[i].get();
  1477. if (probep) // Paranoia
  1478. {
  1479. probep->mLastUpdateTime = 0.f;
  1480. probep->mComplete = false;
  1481. probep->mProbeIndex = -1;
  1482. probep->mCubeArray = NULL;
  1483. probep->mCubeIndex = -1;
  1484. probep->mNeighbors.clear();
  1485. }
  1486. }
  1487. mCubeFree.clear();
  1488. initCubeFree();
  1489. if (mDefaultProbe.isNull())
  1490. {
  1491. // The default probe MUST be the first probe created
  1492. llassert(mProbes.empty());
  1493. mDefaultProbe = new LLReflectionMap();
  1494. mProbes.push_back(mDefaultProbe);
  1495. }
  1496. llassert(mProbes[0] == mDefaultProbe);
  1497. mDefaultProbe->mCubeIndex = 0;
  1498. mDefaultProbe->mCubeArray = mTexture;
  1499. mDefaultProbe->mDistance = 64.f;
  1500. mDefaultProbe->mRadius = 4096.f;
  1501. mDefaultProbe->mProbeIndex = 0;
  1502. mDefaultProbe->mComplete = default_complete;
  1503. touch_default_probe(mDefaultProbe);
  1504. if (mProbeResolution != mOldProbeResolution)
  1505. {
  1506. mOldProbeResolution = mProbeResolution;
  1507. mRenderTarget.release();
  1508. mMipChain.clear();
  1509. }
  1510. }
  1511. if (!mRenderTarget.isComplete())
  1512. {
  1513. U32 tgt_res = mProbeResolution * 4; // Super sample
  1514. mRenderTarget.allocate(tgt_res, tgt_res, GL_RGB16F, true);
  1515. }
  1516. if (mMipChain.empty())
  1517. {
  1518. U32 res = mProbeResolution;
  1519. U32 count = U32(log2f(F32(res)) + 0.5f);
  1520. mMipChain.resize(count);
  1521. for (U32 i = 0; i < count; ++i)
  1522. {
  1523. mMipChain[i].allocate(res, res, GL_RGB16F);
  1524. res /= 2;
  1525. }
  1526. }
  1527. if (mVertexBuffer.isNull())
  1528. {
  1529. constexpr U32 mask = LLVertexBuffer::MAP_VERTEX;
  1530. mVertexBuffer = new LLVertexBuffer(mask);
  1531. mVertexBuffer->allocateBuffer(4, 0);
  1532. LLStrider<LLVector3> v;
  1533. mVertexBuffer->getVertexStrider(v);
  1534. v[0] = LLVector3(-1.f, -1.f, -1.f);
  1535. v[1] = LLVector3(1.f, -1.f, -1.f);
  1536. v[2] = LLVector3(-1.f, 1.f, -1.f);
  1537. v[3] = LLVector3(1.f, 1.f, -1.f);
  1538. mVertexBuffer->unmapBuffer();
  1539. }
  1540. }
  1541. void LLReflectionMapManager::cleanup()
  1542. {
  1543. mVertexBuffer = NULL;
  1544. mRenderTarget.release();
  1545. mMipChain.clear();
  1546. mTexture = NULL;
  1547. mIrradianceMaps = NULL;
  1548. mReflectionProbeCount = 0;
  1549. mProbes.clear();
  1550. mKillList.clear();
  1551. mCreateList.clear();
  1552. mReflectionMaps.clear();
  1553. mUpdatingFace = 0;
  1554. mDefaultProbe = NULL;
  1555. mUpdatingProbe = NULL;
  1556. glDeleteBuffers(1, &mUBO);
  1557. mUBO = 0;
  1558. // Note: also called on teleport (not just shutdown), so make sure we are
  1559. // in a good "starting" state.
  1560. initCubeFree();
  1561. }
  1562. void LLReflectionMapManager::doOcclusion()
  1563. {
  1564. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  1565. if (!gUsePBRShaders)
  1566. {
  1567. return;
  1568. }
  1569. LLVector4a eye;
  1570. eye.load3(gViewerCamera.getOrigin().mV);
  1571. for (size_t i = 0, count = mProbes.size(); i < count; ++i)
  1572. {
  1573. LLReflectionMap* probep = mProbes[i].get();
  1574. if (probep && probep != mDefaultProbe.get())
  1575. {
  1576. probep->doOcclusion(eye);
  1577. }
  1578. }
  1579. }
  1580. void LLReflectionMapManager::forceDefaultProbeAndUpdateUniforms(bool force)
  1581. {
  1582. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  1583. static std::vector<bool> was_occluded;
  1584. if (force)
  1585. {
  1586. was_occluded.clear();
  1587. for (U32 i = 0, count = mProbes.size(); i < count; ++i)
  1588. {
  1589. LLReflectionMap* probep = mProbes[i].get();
  1590. was_occluded.push_back(probep && probep->mOccluded);
  1591. if (probep && probep != mDefaultProbe)
  1592. {
  1593. probep->mOccluded = true;
  1594. }
  1595. }
  1596. updateUniforms();
  1597. }
  1598. else
  1599. {
  1600. for (U32 i = 0, count = llmin(mProbes.size(), was_occluded.size());
  1601. i < count; ++i)
  1602. {
  1603. LLReflectionMap* probep = mProbes[i].get();
  1604. was_occluded.push_back(probep && probep->mOccluded);
  1605. if (probep)
  1606. {
  1607. probep->mOccluded = was_occluded[i];
  1608. }
  1609. }
  1610. was_occluded.clear();
  1611. }
  1612. }
  1613. ///////////////////////////////////////////////////////////////////////////////
  1614. // LLHeroProbeManager class
  1615. ///////////////////////////////////////////////////////////////////////////////
  1616. LLHeroProbeManager::LLHeroProbeManager()
  1617. : mReflectionProbeCount(0),
  1618. mLastUpdateTime(0.f),
  1619. mCurrentProbeUpdateFrame(0),
  1620. mProbeResolution(1024),
  1621. mMaxProbeLOD(6.f),
  1622. mNearestProbeDist(F32_MAX),
  1623. mHeroProbeStrength(1.f),
  1624. mIsInTransition(false),
  1625. mReset(false),
  1626. mRenderingMirror(false)
  1627. {
  1628. }
  1629. LLHeroProbeManager::~LLHeroProbeManager()
  1630. {
  1631. cleanup();
  1632. mHeroVOList.clear();
  1633. mNearestHero = NULL;
  1634. }
  1635. void LLHeroProbeManager::cleanup()
  1636. {
  1637. mVertexBuffer = NULL;
  1638. mRenderTarget.release();
  1639. mMipChain.clear();
  1640. mTexture = NULL;
  1641. mReflectionProbeCount = 0;
  1642. mProbes.clear();
  1643. mDefaultProbe = NULL;
  1644. }
  1645. void LLHeroProbeManager::initReflectionMaps()
  1646. {
  1647. LL_TRACY_TIMER(TRC_HERO_PROBE);
  1648. if (!LLPipeline::sRenderMirrors)
  1649. {
  1650. return;
  1651. }
  1652. if (mReset)
  1653. {
  1654. mReset = false;
  1655. cleanup();
  1656. }
  1657. static LLCachedControl<U32> max_count(gSavedSettings,
  1658. "RenderHeroProbesMaxCount");
  1659. const U32 count = llclamp(U32(max_count), 1, LL_MAX_HERO_PROBE_COUNT);
  1660. if (mTexture.isNull() || mReflectionProbeCount != count)
  1661. {
  1662. mReflectionProbeCount = count;
  1663. static LLCachedControl<U32> probe_level(gSavedSettings,
  1664. "RenderHeroProbeResLevel");
  1665. switch ((U32)probe_level)
  1666. {
  1667. // Note: 0 now actually means hero probe/mirror disabled (256x256
  1668. // is way too small and blurry for a mirror, anyway). HB
  1669. case 0: mProbeResolution = 256; break;
  1670. case 1: mProbeResolution = 512; break;
  1671. case 2: mProbeResolution = 1024; break;
  1672. default: mProbeResolution = 2048; break;
  1673. }
  1674. // Number of mips - 1
  1675. mMaxProbeLOD = log2f(F32(mProbeResolution)) - 1.f;
  1676. mTexture = new LLCubeMapArray();
  1677. // We use two more cube maps for render target and radiance map
  1678. // generation source.
  1679. mTexture->allocate(mProbeResolution, 3, mReflectionProbeCount + 2);
  1680. if (mDefaultProbe.isNull())
  1681. {
  1682. // The default probe MUST be the first probe created
  1683. llassert_always(mProbes.empty());
  1684. mDefaultProbe = new LLReflectionMap();
  1685. mProbes.push_back(mDefaultProbe);
  1686. }
  1687. llassert(mProbes[0] == mDefaultProbe);
  1688. // For hero probes, we treat this as the main mirror probe.
  1689. mDefaultProbe->mCubeIndex = 0;
  1690. mDefaultProbe->mCubeArray = mTexture;
  1691. static LLCachedControl<U32> probe_dist(gSavedSettings,
  1692. "RenderHeroProbeDistance");
  1693. mDefaultProbe->mDistance = llclamp((F32)probe_dist, 2.f, 64.f);
  1694. mDefaultProbe->mRadius = 4096.f;
  1695. mDefaultProbe->mProbeIndex = 0;
  1696. touch_default_probe(mDefaultProbe);
  1697. mProbes.push_back(mDefaultProbe);
  1698. }
  1699. if (mVertexBuffer.isNull())
  1700. {
  1701. constexpr U32 mask = LLVertexBuffer::MAP_VERTEX;
  1702. mVertexBuffer = new LLVertexBuffer(mask);
  1703. mVertexBuffer->allocateBuffer(4, 0);
  1704. LLStrider<LLVector3> v;
  1705. mVertexBuffer->getVertexStrider(v);
  1706. v[0] = LLVector3(-1.f, -1.f, -1.f);
  1707. v[1] = LLVector3(1.f, -1.f, -1.f);
  1708. v[2] = LLVector3(-1.f, 1.f, -1.f);
  1709. v[3] = LLVector3(1.f, 1.f, -1.f);
  1710. mVertexBuffer->unmapBuffer();
  1711. }
  1712. }
  1713. void LLHeroProbeManager::update()
  1714. {
  1715. LL_TRACY_TIMER(TRC_HERO_PROBE);
  1716. mNearestHero = NULL;
  1717. mNearestProbeDist = F32_MAX;
  1718. if (!LLPipeline::sRenderMirrors || gDisconnected ||
  1719. !LLStartUp::isLoggedIn() || gAppViewerp->logoutRequestSent())
  1720. {
  1721. return;
  1722. }
  1723. initReflectionMaps();
  1724. // Probe 0 is always our mirror probe.
  1725. if (mProbes[0].isNull())
  1726. {
  1727. llwarns_sparse << "NULL default hero probe." << llendl;
  1728. return;
  1729. }
  1730. llassert(mProbes[0] == mDefaultProbe);
  1731. if (!mRenderTarget.isComplete())
  1732. {
  1733. mRenderTarget.allocate(mProbeResolution, mProbeResolution, GL_RGB16F,
  1734. true);
  1735. llinfos << "Allocated a render target for mirrors at size: "
  1736. << mProbeResolution << "x" << mProbeResolution << llendl;
  1737. }
  1738. if (mMipChain.empty())
  1739. {
  1740. U32 res = mProbeResolution;
  1741. U32 count = U32(log2f(F32(res)) + 0.5f);
  1742. mMipChain.resize(count);
  1743. for (U32 i = 0; i < count; ++i)
  1744. {
  1745. mMipChain[i].allocate(res, res, GL_RGB16F);
  1746. res /= 2;
  1747. }
  1748. llinfos << "Allocated " << count << " mips for mirrors." << llendl;
  1749. }
  1750. F32 far_cam_dist = gViewerCamera.getFar();
  1751. #if 0 // Wrong axis, LL... HB
  1752. LLVector3 cam_dir = LLVector3::z_axis * gViewerCamera.getQuaternion();
  1753. #else
  1754. LLVector3 cam_dir = LLVector3::x_axis * gViewerCamera.getQuaternion();
  1755. #endif
  1756. const LLVector3& camera_pos = gViewerCamera.getOrigin();
  1757. // Find the closest mirror, if any.
  1758. F32 last_cam_dist = F32_MAX;
  1759. LLVector4a center, size;
  1760. for (volp_set_t::iterator it = mHeroVOList.begin(),
  1761. end = mHeroVOList.end();
  1762. it != end; ++it)
  1763. {
  1764. LLVOVolume* volp = it->get();
  1765. if (!volp || volp->isDead() || volp->mDrawable.isNull() ||
  1766. !volp->isReflectionProbe() || !volp->getReflectionProbeIsBox())
  1767. {
  1768. unregisterViewerObject(volp);
  1769. continue;
  1770. }
  1771. const LLVector3& vol_pos = volp->getPositionAgent();
  1772. LLVector3 offset = camera_pos - vol_pos;
  1773. F32 distance = offset.length();
  1774. if (distance > far_cam_dist || distance > mNearestProbeDist)
  1775. {
  1776. continue;
  1777. }
  1778. F32 cam_dist = cam_dir * -offset;
  1779. if (cam_dist > last_cam_dist)
  1780. {
  1781. continue;
  1782. }
  1783. center.load3(vol_pos.mV);
  1784. size.load3(volp->getScale().mV);
  1785. if (gViewerCamera.AABBInFrustum(center, size))
  1786. {
  1787. // Check to see if the camera is in front of the +Z face of the
  1788. // hero probe (if it is behind, then the probe is not interesting).
  1789. LLVector3 probe_normal = LLVector3::z_axis *
  1790. volp->mDrawable->getWorldRotation();
  1791. #if 0 // Only the sign of the product is interesting... HB
  1792. probe_normal.normalize();
  1793. #endif
  1794. if (probe_normal * offset >= 0.f)
  1795. {
  1796. // Yes, this probe is a candidate
  1797. mNearestProbeDist = distance;
  1798. last_cam_dist = cam_dist;
  1799. mNearestHero = volp;
  1800. }
  1801. }
  1802. }
  1803. // No close, visible and valid/live hero probe. Abort.
  1804. if (mNearestHero.isNull() || mNearestHero->isDead() ||
  1805. mNearestHero->mDrawable.isNull())
  1806. {
  1807. // Set the tracked object for this probe to NULL.
  1808. mProbes[0]->mViewerObject = NULL;
  1809. mNearestHero = NULL;
  1810. mNearestProbeDist = F32_MAX;
  1811. return;
  1812. }
  1813. // Update the tracked object for this probe.
  1814. mProbes[0]->mViewerObject = mNearestHero;
  1815. mMirrorPosition = mNearestHero->getPositionAgent();
  1816. mMirrorNormal.set(0.f, 0.f, 1.f);
  1817. mMirrorNormal *= mNearestHero->mDrawable->getWorldRotation();
  1818. mMirrorNormal.normalize();
  1819. mCurrentClipPlane.setVec(mMirrorPosition, mMirrorNormal);
  1820. LLVector3 offset = camera_pos - mMirrorPosition;
  1821. LLVector3 project = mMirrorNormal * (offset * mMirrorNormal);
  1822. LLVector3 reject = offset - project;
  1823. LLVector3 point = (reject - project) + mMirrorPosition;
  1824. mProbes[0]->mOrigin.load3(point.mV);
  1825. // Detect visible faces of a cube based on camera direction and distance.
  1826. static const LLVector3 faces[6] =
  1827. {
  1828. LLVector3::x_axis,
  1829. LLVector3::x_axis_neg,
  1830. LLVector3::y_axis,
  1831. LLVector3::y_axis_neg,
  1832. LLVector3::z_axis,
  1833. LLVector3::z_axis_neg
  1834. };
  1835. static LLCachedControl<U32> mult(gSavedSettings,
  1836. "RenderHeroProbeUpdateMult");
  1837. F32 update_mult = llclamp((F32)mult, 2.f, 64.f);
  1838. for (U32 i = 0; i < 6; ++i)
  1839. {
  1840. F32 cube_facing = 1.f - llclamp(cam_dir * faces[i], -1.f, 1.f);
  1841. mFaceUpdateList[i] = (S32)llceil(cube_facing * update_mult);
  1842. }
  1843. mHeroProbeStrength = 1.f;
  1844. }
  1845. void LLHeroProbeManager::renderProbes()
  1846. {
  1847. if (mNearestHero.isNull())
  1848. {
  1849. return; // Nothing to do.
  1850. }
  1851. static LLCachedControl<U32> fps(gSavedSettings, "RenderHeroProbeMaxFPS");
  1852. static LLCachedControl<U32> interval(gSavedSettings,
  1853. "RenderHeroProbeMinFrameInterval");
  1854. F32 delta = gFrameTimeSeconds - mLastUpdateTime;
  1855. if (delta < 1.f / llclamp((F32)fps, 1.f, 60.f) ||
  1856. delta < gFrameIntervalSeconds * (F32)interval)
  1857. {
  1858. return;
  1859. }
  1860. mLastUpdateTime = gFrameTimeSeconds;
  1861. // Do not update mips when the nearest probe is beyond a distance from the
  1862. // camera determined by the probe mDistance and a user-configurable cut-off
  1863. // factor. This avoids excessive GPU consumption and lowered FPS rates when
  1864. // there is no mirror close to the camera, or even no mirror currently
  1865. // rendered at all ! HB
  1866. static LLCachedControl<F32> cutoff(gSavedSettings,
  1867. "RenderHeroProbeCutoff");
  1868. if (cutoff > 1.f && mNearestProbeDist > mProbes[0]->mDistance * cutoff)
  1869. {
  1870. mProbes[0]->autoAdjustOrigin(); // Still adjust the position...
  1871. return;
  1872. }
  1873. constexpr F32 NEAR_CLIP = 0.1f;
  1874. bool is_dynamic = mNearestHero->getReflectionProbeIsDynamic();
  1875. bool radiance_pass = gPipeline.mReflectionMapManager.isRadiancePass();
  1876. gPipeline.mReflectionMapManager.mRadiancePass = true;
  1877. mRenderingMirror = true;
  1878. doOcclusion();
  1879. for (size_t j = 0, count = mProbes.size(); j < count; ++j)
  1880. {
  1881. for (size_t i = 0; i < 6; ++i)
  1882. {
  1883. if (mFaceUpdateList[i] > 0 &&
  1884. !(mCurrentProbeUpdateFrame % mFaceUpdateList[i]))
  1885. {
  1886. updateProbeFace(mProbes[j], i, is_dynamic, NEAR_CLIP);
  1887. mCurrentProbeUpdateFrame = 0;
  1888. }
  1889. }
  1890. generateRadiance(mProbes[j]);
  1891. }
  1892. mRenderingMirror = false;
  1893. gPipeline.mReflectionMapManager.mRadiancePass = radiance_pass;
  1894. mProbes[0]->autoAdjustOrigin();
  1895. ++mCurrentProbeUpdateFrame;
  1896. }
  1897. // Does the reflection map update render passes. For every 12 calls of this
  1898. // method, one complete reflection probe radiance map and irradiance map is
  1899. // generated. First 6 passes render the scene with direct lighting only into a
  1900. // scratch space cube map at the end of the cube a simple mip chain (not
  1901. // convolution filter). At the end of these passes, an irradiance map is
  1902. // generated for this probe and placed into the irradiance cube map array at
  1903. // the index for this probe. The next six passes render the scene with both
  1904. // radiance and irradiance into the same scratch space cube map and generate
  1905. // a simple mip chain. Finally, a radiance map is generated for this probe and
  1906. // placed into the radiance cube map array at the index for this probe. In
  1907. // effect this simulates single-bounce lighting.
  1908. void LLHeroProbeManager::updateProbeFace(LLReflectionMap* probep, U32 face,
  1909. bool is_dynamic, F32 near_clip)
  1910. {
  1911. LL_TRACY_TIMER(TRC_HERO_PROBE);
  1912. // Hacky hot-swap of camera specific render targets
  1913. auto saved_rt = gPipeline.mRT;
  1914. gPipeline.mRT = &gPipeline.mHeroProbeRT;
  1915. probep->update(mRenderTarget.getWidth(), face, is_dynamic, near_clip);
  1916. gPipeline.mRT = saved_rt;
  1917. gGL.setColorMask(true, true);
  1918. LLGLDepthTest depth(GL_FALSE, GL_FALSE);
  1919. LLGLDisable cull(GL_CULL_FACE);
  1920. LLGLDisable blend(GL_BLEND);
  1921. // Downsample to placeholder map
  1922. gGL.matrixMode(LLRender::MM_MODELVIEW);
  1923. gGL.pushMatrix();
  1924. gGL.loadIdentity();
  1925. gGL.matrixMode(LLRender::MM_PROJECTION);
  1926. gGL.pushMatrix();
  1927. gGL.loadIdentity();
  1928. gGL.flush();
  1929. LLRenderTarget* screenp = &gPipeline.mHeroProbeRT.mScreen;
  1930. // Perform a gaussian blur on the super sampled render before downsampling
  1931. LLGLSLShader* shaderp = &gGaussianProgram;
  1932. shaderp->bind();
  1933. shaderp->uniform1f(sResScale, 0.5f / F32(mProbeResolution));
  1934. S32 chan = shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  1935. LLTexUnit::TT_TEXTURE);
  1936. // Horizontal
  1937. shaderp->uniform2f(sDirection, 1.f, 0.f);
  1938. gGL.getTexUnit(chan)->bind(screenp);
  1939. mRenderTarget.bindTarget();
  1940. gPipeline.mScreenTriangleVB->setBuffer();
  1941. gPipeline.mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  1942. mRenderTarget.flush();
  1943. // Vertical
  1944. shaderp->uniform2f(sDirection, 0.f, 1.f);
  1945. gGL.getTexUnit(chan)->bind(&mRenderTarget);
  1946. screenp->bindTarget();
  1947. gPipeline.mScreenTriangleVB->setBuffer();
  1948. gPipeline.mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  1949. screenp->flush();
  1950. shaderp->unbind();
  1951. // Unlike the reflectionmap manager, all probes are considered "realtime"
  1952. // for hero probes.
  1953. U32 src_idx = mReflectionProbeCount + 1;
  1954. S32 mips = S32(log2f((F32)mProbeResolution) + 0.5f);
  1955. shaderp = &gReflectionMipProgram;
  1956. shaderp->bind();
  1957. chan = shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  1958. LLTexUnit::TT_TEXTURE);
  1959. LLTexUnit* diffunitp = gGL.getTexUnit(chan);
  1960. U32 res = mProbeResolution;
  1961. for (S32 i = 0, count = mMipChain.size(); i < count; ++i)
  1962. {
  1963. mMipChain[i].bindTarget();
  1964. diffunitp->bind(i ? &(mMipChain[i - 1]) : screenp);
  1965. shaderp->uniform1f(sResScale, 0.5f / F32(mProbeResolution));
  1966. shaderp->uniform1f(sZnear, probep->getNearClip());
  1967. shaderp->uniform1f(sZfar, MAX_FAR_CLIP);
  1968. gPipeline.mScreenTriangleVB->setBuffer();
  1969. gPipeline.mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  1970. S32 mip = i - count + mips;
  1971. if (mip >= 0)
  1972. {
  1973. mTexture->bind(0);
  1974. glCopyTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, mip, 0, 0,
  1975. src_idx * 6 + face, 0, 0, res, res);
  1976. mTexture->unbind();
  1977. }
  1978. res /= 2;
  1979. mMipChain[i].flush();
  1980. }
  1981. gGL.popMatrix();
  1982. gGL.matrixMode(LLRender::MM_MODELVIEW);
  1983. gGL.popMatrix();
  1984. if (diffunitp)
  1985. {
  1986. diffunitp->unbind(LLTexUnit::TT_TEXTURE);
  1987. }
  1988. shaderp->unbind();
  1989. }
  1990. // Radiance generation is done in a separate stage; this is to better enable
  1991. // independent control over how we generate radiance vs. having it coupled with
  1992. // processing the final face of the probe. Useful when we may not always be
  1993. // rendering a full set of faces of the probe.
  1994. void LLHeroProbeManager::generateRadiance(LLReflectionMap* probep)
  1995. {
  1996. LL_TRACY_TIMER(TRC_HERO_PROBE);
  1997. // Unlike the reflectionmap manager, all probes are considered "realtime"
  1998. // for hero probes.
  1999. U32 src_idx = mReflectionProbeCount + 1;
  2000. mMipChain[0].bindTarget();
  2001. // Generate radiance map (even if this is not the irradiance map, we need
  2002. // the mip chain for the irradiance map).
  2003. LLGLSLShader& shader = gHeroRadianceGenProgram;
  2004. shader.bind();
  2005. mVertexBuffer->setBuffer();
  2006. S32 chan = shader.enableTexture(LLShaderMgr::REFLECTION_PROBES,
  2007. LLTexUnit::TT_CUBE_MAP_ARRAY);
  2008. mTexture->bind(chan);
  2009. shader.uniform1i(sSourceIdx, src_idx);
  2010. shader.uniform1f(LLShaderMgr::REFLECTION_PROBE_MAX_LOD, mMaxProbeLOD);
  2011. shader.uniform1f(LLShaderMgr::REFLECTION_PROBE_STRENGTH,
  2012. mHeroProbeStrength);
  2013. U32 res = mMipChain[0].getWidth();
  2014. LLCoordFrame frame;
  2015. F32 mat[16];
  2016. size_t mips_chain_size = mMipChain.size();
  2017. F32 roughness_factor = 1.f / F32(mips_chain_size - 1);
  2018. for (size_t i = 0, count = mips_chain_size / 4; i < count; ++i)
  2019. {
  2020. shader.uniform1f(sRoughness, (F32)i * roughness_factor);
  2021. shader.uniform1f(sMipLevel, (F32)i);
  2022. shader.uniform1i(sWidth, mProbeResolution);
  2023. shader.uniform1f(sStrength, 1.f);
  2024. // For each cube face
  2025. for (U32 cf = 0; cf < 6; ++cf)
  2026. {
  2027. frame.lookAt(LLVector3::zero,
  2028. LLCubeMapArray::sClipToCubeLookVecs[cf],
  2029. LLCubeMapArray::sClipToCubeUpVecs[cf]);
  2030. frame.getOpenGLRotation(mat);
  2031. gGL.loadMatrix(mat);
  2032. mVertexBuffer->drawArrays(gGL.TRIANGLE_STRIP, 0, 4);
  2033. glCopyTexSubImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, i, 0, 0,
  2034. probep->mCubeIndex * 6 + cf, 0, 0,
  2035. res, res);
  2036. }
  2037. if (i != count - 1)
  2038. {
  2039. res /= 2;
  2040. glViewport(0, 0, res, res);
  2041. }
  2042. }
  2043. shader.unbind();
  2044. mMipChain[0].flush();
  2045. }
  2046. void LLHeroProbeManager::updateUniforms()
  2047. {
  2048. LL_TRACY_TIMER(TRC_HERO_PROBE);
  2049. if (!LLPipeline::sRenderMirrors)
  2050. {
  2051. mHeroData.heroProbeCount = 0;
  2052. return;
  2053. }
  2054. mHeroData.heroProbeCount = 1;
  2055. if (mNearestHero.notNull() && !mNearestHero->isDead() &&
  2056. mDefaultProbe.notNull())
  2057. {
  2058. LLMatrix4a modelview = gGLModelView;
  2059. LLVector4a oa; // Scratch space for transformed origin
  2060. oa.set(0.f, 0.f, 0.f, 0.f);
  2061. F32 radius;
  2062. if (mNearestHero->getReflectionProbeIsBox())
  2063. {
  2064. static const LLVector3 half(0.5f, 0.5f, 0.5f);
  2065. radius = mNearestHero->getScale().scaledVec(half).length();
  2066. }
  2067. else
  2068. {
  2069. radius = mNearestHero->getScale().mV[0] * 0.5f;
  2070. }
  2071. mDefaultProbe->mRadius = radius;
  2072. modelview.affineTransform(mDefaultProbe->mOrigin, oa);
  2073. mHeroData.heroSphere.set(oa.getF32ptr());
  2074. mHeroData.heroSphere.mV[3] = radius;
  2075. mHeroData.heroShape = mDefaultProbe->getBox(mHeroData.heroBox) ? 0 : 1;
  2076. }
  2077. mHeroData.heroMipCount = mMipChain.size();
  2078. }
  2079. void LLHeroProbeManager::renderDebug()
  2080. {
  2081. LL_TRACY_TIMER(TRC_HERO_PROBE);
  2082. gDebugProgram.bind();
  2083. for (size_t i = 0, count = mProbes.size(); i < count; ++i)
  2084. {
  2085. render_reflection_probe(mProbes[i].get());
  2086. }
  2087. gDebugProgram.unbind();
  2088. }
  2089. void LLHeroProbeManager::doOcclusion()
  2090. {
  2091. LL_TRACY_TIMER(TRC_REFLECTION_PROBE);
  2092. if (!gUsePBRShaders)
  2093. {
  2094. return;
  2095. }
  2096. LLVector4a eye;
  2097. eye.load3(gViewerCamera.getOrigin().mV);
  2098. for (size_t i = 0, count = mProbes.size(); i < count; ++i)
  2099. {
  2100. LLReflectionMap* probep = mProbes[i].get();
  2101. if (probep && probep != mDefaultProbe.get())
  2102. {
  2103. probep->doOcclusion(eye);
  2104. }
  2105. }
  2106. }
  2107. bool LLHeroProbeManager::registerViewerObject(LLVOVolume* volp)
  2108. {
  2109. if (!volp || volp->isDead() || volp->mDrawable.isNull())
  2110. {
  2111. return false; // Nope, we did not register this ! HB
  2112. }
  2113. mHeroVOList.emplace(volp);
  2114. if (volp)
  2115. {
  2116. volp->mIsHeroProbe = true;
  2117. }
  2118. return true; // It is now indeed in our list. HB
  2119. }
  2120. void LLHeroProbeManager::unregisterViewerObject(LLVOVolume* volp)
  2121. {
  2122. mHeroVOList.erase(volp);
  2123. if (volp)
  2124. {
  2125. volp->mIsHeroProbe = false;
  2126. }
  2127. }