lldrawpoolterrain.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /**
  2. * @file lldrawpoolterrain.cpp
  3. * @brief LLDrawPoolTerrain class implementation
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-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 "lldrawpoolterrain.h"
  34. #include "imageids.h"
  35. #include "llfasttimer.h"
  36. #include "llrender.h"
  37. #include "lldrawable.h"
  38. #include "llenvironment.h"
  39. #include "llenvsettings.h"
  40. #include "llface.h"
  41. #include "llpipeline.h"
  42. //MK
  43. #include "mkrlinterface.h"
  44. //mk
  45. #include "llsky.h"
  46. #include "llsurface.h"
  47. #include "llsurfacepatch.h"
  48. #include "llviewercamera.h"
  49. #include "llviewercontrol.h"
  50. #include "llviewerparcelmgr.h" // For gRenderParcelOwnership
  51. #include "llviewerparceloverlay.h"
  52. #include "llviewerregion.h"
  53. #include "llviewershadermgr.h"
  54. #include "llviewertexturelist.h" // To get alpha gradients
  55. #include "llvlcomposition.h"
  56. #include "llvosurfacepatch.h"
  57. #include "llworld.h"
  58. constexpr F32 DETAIL_SCALE = 1.f / 16.f;
  59. int DebugDetailMap = 0;
  60. F32 LLDrawPoolTerrain::sDetailScale = DETAIL_SCALE;
  61. static LLGLSLShader* sShader = NULL;
  62. LLDrawPoolTerrain::LLDrawPoolTerrain(LLViewerTexture* texturep)
  63. : LLFacePool(POOL_TERRAIN),
  64. mTexturep(texturep)
  65. {
  66. static LLCachedControl<F32> terrain_scale(gSavedSettings,
  67. "RenderTerrainScale");
  68. sDetailScale = 1.f / llmax(0.1f, (F32)terrain_scale);
  69. mAlphaRampImagep =
  70. LLViewerTextureManager::getFetchedTexture(IMG_ALPHA_GRAD);
  71. mAlphaRampImagep->setAddressMode(LLTexUnit::TAM_CLAMP);
  72. m2DAlphaRampImagep =
  73. LLViewerTextureManager::getFetchedTexture(IMG_ALPHA_GRAD_2D);
  74. m2DAlphaRampImagep->setAddressMode(LLTexUnit::TAM_CLAMP);
  75. if (mTexturep)
  76. {
  77. mTexturep->setBoostLevel(LLGLTexture::BOOST_TERRAIN);
  78. }
  79. }
  80. //virtual
  81. U32 LLDrawPoolTerrain::getVertexDataMask()
  82. {
  83. if (LLPipeline::sShadowRender)
  84. {
  85. return LLVertexBuffer::MAP_VERTEX;
  86. }
  87. U32 mask = VERTEX_DATA_MASK;
  88. if (gUsePBRShaders)
  89. {
  90. // We only need tangents for PBR terrains.
  91. mask = VERTEX_DATA_MASK | LLVertexBuffer::MAP_TANGENT;
  92. }
  93. if (LLGLSLShader::sCurBoundShaderPtr)
  94. {
  95. return mask & ~(LLVertexBuffer::MAP_TEXCOORD2 |
  96. LLVertexBuffer::MAP_TEXCOORD3);
  97. }
  98. return mask;
  99. }
  100. //virtual
  101. void LLDrawPoolTerrain::prerender()
  102. {
  103. mShaderLevel =
  104. gViewerShaderMgrp->getShaderLevel(LLViewerShaderMgr::SHADER_ENVIRONMENT);
  105. }
  106. // For use by the EE renderer only
  107. //virtual
  108. void LLDrawPoolTerrain::beginRenderPass(S32)
  109. {
  110. LL_FAST_TIMER(FTM_RENDER_TERRAIN);
  111. sShader = LLPipeline::sUnderWaterRender ? &gTerrainWaterProgram
  112. : &gTerrainProgram;
  113. if (mShaderLevel > 1 && sShader->mShaderLevel > 0)
  114. {
  115. sShader->bind();
  116. }
  117. }
  118. // For use by the EE renderer only
  119. //virtual
  120. void LLDrawPoolTerrain::endRenderPass(S32 pass)
  121. {
  122. LL_FAST_TIMER(FTM_RENDER_TERRAIN);
  123. if (mShaderLevel > 1 && sShader->mShaderLevel > 0)
  124. {
  125. sShader->unbind();
  126. }
  127. }
  128. // For use by the EE renderer only
  129. //virtual
  130. void LLDrawPoolTerrain::render(S32 pass)
  131. {
  132. LL_FAST_TIMER(FTM_RENDER_TERRAIN);
  133. if (mDrawFace.empty())
  134. {
  135. return;
  136. }
  137. // HB. Useless to do it at every render pass...
  138. // FIXME: see to move this to LLVLComposition so to avoid re-doing it at
  139. // each frame !!!
  140. if (pass == 0)
  141. {
  142. boostTerrainDetailTextures();
  143. }
  144. LLOverrideFaceColor color_override(this, 1.f, 1.f, 1.f, 1.f);
  145. // Render simplified land if video card cannot do sufficient multitexturing
  146. if (gGLManager.mNumTextureImageUnits < 2)
  147. {
  148. renderSimple(); // Render without multitexture
  149. return;
  150. }
  151. LLGLSPipeline gls;
  152. if (mShaderLevel > 1 && sShader->mShaderLevel > 0)
  153. {
  154. gPipeline.enableLightsDynamic();
  155. renderFullShader();
  156. }
  157. else
  158. {
  159. gPipeline.enableLightsStatic();
  160. if (gGLManager.mNumTextureImageUnits < 4)
  161. {
  162. renderFull2TU();
  163. }
  164. else
  165. {
  166. renderFull4TU();
  167. }
  168. }
  169. // Special case for land ownership feedback
  170. static LLCachedControl<bool> show_parcel_owners(gSavedSettings,
  171. "ShowParcelOwners");
  172. if (show_parcel_owners)
  173. {
  174. hilightParcelOwners();
  175. }
  176. }
  177. //virtual
  178. void LLDrawPoolTerrain::beginDeferredPass(S32)
  179. {
  180. LL_FAST_TIMER(FTM_RENDER_TERRAIN);
  181. if (!gUsePBRShaders)
  182. {
  183. if (LLPipeline::sUnderWaterRender)
  184. {
  185. sShader = &gDeferredTerrainWaterProgram;
  186. }
  187. else
  188. {
  189. sShader = &gDeferredTerrainProgram;
  190. }
  191. sShader->bind();
  192. }
  193. }
  194. //virtual
  195. void LLDrawPoolTerrain::endDeferredPass(S32 pass)
  196. {
  197. LL_FAST_TIMER(FTM_RENDER_TERRAIN);
  198. LLFacePool::endRenderPass(pass);
  199. sShader->unbind();
  200. }
  201. //virtual
  202. void LLDrawPoolTerrain::renderDeferred(S32 pass)
  203. {
  204. LL_FAST_TIMER(FTM_RENDER_TERRAIN);
  205. if (mDrawFace.empty())
  206. {
  207. return;
  208. }
  209. if (pass == 0 && gUsePBRShaders)
  210. {
  211. boostTerrainDetailTextures();
  212. }
  213. renderFullShader();
  214. // Special case for land ownership feedback
  215. static LLCachedControl<bool> show_parcel_owners(gSavedSettings,
  216. "ShowParcelOwners");
  217. if (show_parcel_owners)
  218. {
  219. hilightParcelOwners();
  220. }
  221. }
  222. //virtual
  223. void LLDrawPoolTerrain::beginShadowPass(S32)
  224. {
  225. LL_FAST_TIMER(FTM_SHADOW_TERRAIN);
  226. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  227. gDeferredShadowProgram.bind();
  228. gDeferredShadowProgram.uniform1i(LLShaderMgr::SUN_UP_FACTOR,
  229. gEnvironment.getIsSunUp() ? 1 : 0);
  230. }
  231. //virtual
  232. void LLDrawPoolTerrain::endShadowPass(S32 pass)
  233. {
  234. LL_FAST_TIMER(FTM_SHADOW_TERRAIN);
  235. LLFacePool::endRenderPass(pass);
  236. gDeferredShadowProgram.unbind();
  237. }
  238. //virtual
  239. void LLDrawPoolTerrain::renderShadow(S32 pass)
  240. {
  241. LL_FAST_TIMER(FTM_SHADOW_TERRAIN);
  242. if (mDrawFace.empty())
  243. {
  244. return;
  245. }
  246. #if 0
  247. LLGLEnable offset(GL_POLYGON_OFFSET);
  248. glCullFace(GL_FRONT);
  249. #endif
  250. drawLoop();
  251. #if 0
  252. glCullFace(GL_BACK);
  253. #endif
  254. }
  255. void LLDrawPoolTerrain::drawLoop()
  256. {
  257. for (U32 i = 0, count = mDrawFace.size(); i < count; ++i)
  258. {
  259. LLFace* facep = mDrawFace[i];
  260. if (!facep) continue; // Paranoia
  261. LLDrawable* drawablep = facep->getDrawable();
  262. if (!drawablep) continue;
  263. LLViewerRegion* regionp = drawablep->getRegion();
  264. if (!regionp) continue;
  265. LLRenderPass::applyModelMatrix(&(regionp->mRenderMatrix));
  266. facep->renderIndexed();
  267. }
  268. }
  269. void LLDrawPoolTerrain::renderFullShader()
  270. {
  271. //MK
  272. if (gRLenabled && gRLInterface.mContainsCamTextures)
  273. {
  274. if (!gUsePBRShaders && LLPipeline::sUnderWaterRender)
  275. {
  276. sShader = &gDeferredTerrainWaterProgram;
  277. }
  278. else
  279. {
  280. sShader = &gDeferredTerrainProgram;
  281. }
  282. renderSimple();
  283. return;
  284. }
  285. //mk
  286. LLViewerRegion* regionp =
  287. mDrawFace[0]->getDrawable()->getVObj()->getRegion();
  288. if (!regionp) return; // Paranoia
  289. if (gUsePBRShaders)
  290. {
  291. // Late PBR shader setup (used to be set in beginDeferredPass(), now
  292. // reserved to ALM), to be able to render both PBR and non-PBR terrains
  293. // during the same render frame in PBR rendering mode. HB
  294. if (LLPipeline::sRenderPBRTerrain &&
  295. regionp->getComposition()->isPBR())
  296. {
  297. sShader = &gDeferredPBRTerrainProgram;
  298. sShader->bind();
  299. renderFullShaderPBR(regionp);
  300. }
  301. else
  302. {
  303. sShader = &gDeferredTerrainProgram;
  304. sShader->bind();
  305. renderFullShaderTextures(regionp);
  306. }
  307. }
  308. else
  309. {
  310. renderFullShaderTextures(regionp);
  311. }
  312. }
  313. void LLDrawPoolTerrain::renderFullShaderTextures(LLViewerRegion* regionp)
  314. {
  315. static std::vector<LLViewerTexture*> textures;
  316. LLVLComposition* compp = regionp->getComposition();
  317. // *HACK: when the terrain is a PBR one, this will return the base color
  318. // textures, allowing to render something better than grey terrain, when
  319. // not in PBR rendering mode. HB
  320. compp->getTextures(textures);
  321. const F64 divisor = 1.0 / (F64)sDetailScale;
  322. LLVector3d region_origin_global = regionp->getOriginGlobal();
  323. F32 offset_x = (F32)fmod(region_origin_global.mdV[VX], divisor) *
  324. sDetailScale;
  325. F32 offset_y = (F32)fmod(region_origin_global.mdV[VY], divisor) *
  326. sDetailScale;
  327. static LLVector4 tp0, tp1;
  328. tp0.set(sDetailScale, 0.f, 0.f, offset_x);
  329. tp1.set(0.f, sDetailScale, 0.f, offset_y);
  330. // Detail texture 0
  331. S32 detail0 = sShader->enableTexture(LLShaderMgr::TERRAIN_DETAIL0);
  332. LLTexUnit* unitdet0 = gGL.getTexUnit(detail0);
  333. unitdet0->bind(textures[0]);
  334. // *BUG: why LL's EEP viewer needs this while it ruins it for us ?
  335. //unitdet0->setTextureAddressMode(LLTexUnit::TAM_WRAP);
  336. unitdet0->activate();
  337. LLGLSLShader* shader = LLGLSLShader::sCurBoundShaderPtr;
  338. llassert(shader);
  339. shader->uniform4fv(LLShaderMgr::OBJECT_PLANE_S, 1, tp0.mV);
  340. shader->uniform4fv(LLShaderMgr::OBJECT_PLANE_T, 1, tp1.mV);
  341. // Detail texture 1
  342. S32 detail1 = sShader->enableTexture(LLShaderMgr::TERRAIN_DETAIL1);
  343. LLTexUnit* unitdet1 = gGL.getTexUnit(detail1);
  344. unitdet1->bind(textures[1]);
  345. // *BUG: why LL's EEP viewer needs this while it ruins it for us ?
  346. //unitdet1->setTextureAddressMode(LLTexUnit::TAM_WRAP);
  347. unitdet1->activate();
  348. S32 detail2 = sShader->enableTexture(LLShaderMgr::TERRAIN_DETAIL2);
  349. LLTexUnit* unitdet2 = gGL.getTexUnit(detail2);
  350. unitdet2->bind(textures[2]);
  351. // *BUG: why LL's EEP viewer needs this while it ruins it for us ?
  352. //unitdet2->setTextureAddressMode(LLTexUnit::TAM_WRAP);
  353. unitdet2->activate();
  354. S32 detail3 = sShader->enableTexture(LLShaderMgr::TERRAIN_DETAIL3);
  355. LLTexUnit* unitdet3 = gGL.getTexUnit(detail3);
  356. unitdet3->bind(textures[3]);
  357. // *BUG: why LL's EEP viewer needs this while it ruins it for us ?
  358. //unitdet3->setTextureAddressMode(LLTexUnit::TAM_WRAP);
  359. unitdet3->activate();
  360. S32 alpha_ramp = sShader->enableTexture(LLShaderMgr::TERRAIN_ALPHARAMP);
  361. LLTexUnit* unitalpha = gGL.getTexUnit(alpha_ramp);
  362. unitalpha->bind(m2DAlphaRampImagep);
  363. // *BUG: why LL's EEP viewer needs this while it ruins it for us ?
  364. //unitalpha->setTextureAddressMode(LLTexUnit::TAM_WRAP);
  365. unitalpha->activate();
  366. // GL_BLEND disabled by default
  367. drawLoop();
  368. // Disable multitexture
  369. sShader->disableTexture(LLShaderMgr::TERRAIN_ALPHARAMP);
  370. sShader->disableTexture(LLShaderMgr::TERRAIN_DETAIL0);
  371. sShader->disableTexture(LLShaderMgr::TERRAIN_DETAIL1);
  372. sShader->disableTexture(LLShaderMgr::TERRAIN_DETAIL2);
  373. sShader->disableTexture(LLShaderMgr::TERRAIN_DETAIL3);
  374. unitalpha->unbind(LLTexUnit::TT_TEXTURE);
  375. unitalpha->disable();
  376. unitalpha->activate();
  377. unitdet3->unbind(LLTexUnit::TT_TEXTURE);
  378. unitdet3->disable();
  379. unitdet3->activate();
  380. unitdet2->unbind(LLTexUnit::TT_TEXTURE);
  381. unitdet2->disable();
  382. unitdet2->activate();
  383. unitdet1->unbind(LLTexUnit::TT_TEXTURE);
  384. unitdet1->disable();
  385. unitdet1->activate();
  386. // Restore texture unit detail0 defaults
  387. unitdet0->unbind(LLTexUnit::TT_TEXTURE);
  388. unitdet0->enable(LLTexUnit::TT_TEXTURE);
  389. unitdet0->activate();
  390. }
  391. void LLDrawPoolTerrain::renderFullShaderPBR(LLViewerRegion* regionp)
  392. {
  393. static std::vector<LLGLTFMaterial*> materials;
  394. constexpr U32 MAT_COUNT = LLVLComposition::ASSET_COUNT;
  395. static S32 detail_basecolor[MAT_COUNT];
  396. static S32 detail_normal[MAT_COUNT];
  397. static S32 detail_mrough[MAT_COUNT];
  398. static S32 detail_emissive[MAT_COUNT];
  399. LLVLComposition* compp = regionp->getComposition();
  400. compp->getGLTFMaterials(materials);
  401. S32 detail_mode = LLPipeline::sRenderPBRTerrainDetail;
  402. LLTexUnit* basecolor_unitp = NULL;
  403. LLTexUnit* normal_unitp = NULL;
  404. LLTexUnit* mrough_unitp = NULL;
  405. LLTexUnit* emissive_unitp = NULL;
  406. for (U32 i = 0; i < MAT_COUNT; ++i)
  407. {
  408. LLViewerTexture* basecolor_texp = NULL;
  409. LLViewerTexture* normal_texp = NULL;
  410. LLViewerTexture* mrough_texp = NULL;
  411. LLViewerTexture* emissive_texp = NULL;
  412. const LLFetchedGLTFMaterial* matp = compp->getFetchedMaterial(i);
  413. if (matp)
  414. {
  415. basecolor_texp = matp->mBaseColorTexture;
  416. normal_texp = matp->mNormalTexture;
  417. mrough_texp = matp->mMetallicRoughnessTexture;
  418. emissive_texp = matp->mEmissiveTexture;
  419. }
  420. detail_basecolor[i] =
  421. sShader->enableTexture(LLShaderMgr::TERRAIN_DETAIL0_BASE_COLOR + i);
  422. basecolor_unitp = gGL.getTexUnit(detail_basecolor[i]);
  423. if (basecolor_texp)
  424. {
  425. basecolor_unitp->bind(basecolor_texp);
  426. }
  427. else
  428. {
  429. basecolor_unitp->bind(LLViewerFetchedTexture::sWhiteImagep);
  430. }
  431. basecolor_unitp->setTextureAddressMode(LLTexUnit::TAM_WRAP);
  432. basecolor_unitp->activate();
  433. if (detail_mode >= TERRAIN_PBR_DETAIL_NORMAL)
  434. {
  435. detail_normal[i] =
  436. sShader->enableTexture(LLShaderMgr::TERRAIN_DETAIL0_NORMAL + i);
  437. normal_unitp = gGL.getTexUnit(detail_normal[i]);
  438. if (normal_texp)
  439. {
  440. normal_unitp->bind(normal_texp);
  441. }
  442. else
  443. {
  444. normal_unitp->bind(LLViewerFetchedTexture::sFlatNormalImagep);
  445. }
  446. normal_unitp->setTextureAddressMode(LLTexUnit::TAM_WRAP);
  447. normal_unitp->activate();
  448. }
  449. if (detail_mode >= TERRAIN_PBR_DETAIL_METALLIC_ROUGHNESS)
  450. {
  451. detail_mrough[i] =
  452. sShader->enableTexture(LLShaderMgr::TERRAIN_DETAIL0_MROUGHNESS + i);
  453. mrough_unitp = gGL.getTexUnit(detail_mrough[i]);
  454. if (mrough_texp)
  455. {
  456. mrough_unitp->bind(mrough_texp);
  457. }
  458. else
  459. {
  460. mrough_unitp->bind(LLViewerFetchedTexture::sWhiteImagep);
  461. }
  462. mrough_unitp->setTextureAddressMode(LLTexUnit::TAM_WRAP);
  463. mrough_unitp->activate();
  464. }
  465. if (detail_mode >= TERRAIN_PBR_DETAIL_EMISSIVE)
  466. {
  467. detail_emissive[i] =
  468. sShader->enableTexture(LLShaderMgr::TERRAIN_DETAIL0_EMISSIVE + i);
  469. emissive_unitp = gGL.getTexUnit(detail_emissive[i]);
  470. if (emissive_texp)
  471. {
  472. emissive_unitp->bind(emissive_texp);
  473. }
  474. else
  475. {
  476. emissive_unitp->bind(LLViewerFetchedTexture::sWhiteImagep);
  477. }
  478. emissive_unitp->setTextureAddressMode(LLTexUnit::TAM_WRAP);
  479. emissive_unitp->activate();
  480. }
  481. }
  482. LLGLSLShader* shader = LLGLSLShader::sCurBoundShaderPtr;
  483. llassert(shader);
  484. static LLCachedControl<F32> terrain_scale(gSavedSettings,
  485. "RenderTerrainPBRScale");
  486. F32 scale = 1.f / llmax(1.f, terrain_scale);
  487. static LLGLTFMaterial::TextureTransform basecolor_trans;
  488. basecolor_trans.mScale = LLVector2(scale, scale);
  489. #if 0 // *TODO: mOffset and mRotation left at defaults for now.
  490. LLVector3d region_origin_global = regionp->getOriginGlobal();
  491. F64 inv_scale = 1.0 / F64(scale);
  492. F32 offset_x = F32(fmod(region_origin_global.mdV[VX], inv_scale)) * scale;
  493. F32 offset_y = F32(fmod(region_origin_global.mdV[VY], inv_scale)) * scale;
  494. basecolor_trans.mOffset = LLVector2(offset_x, offset_y);
  495. #endif
  496. static F32 basecolor_packed[8];
  497. basecolor_trans.getPacked(basecolor_packed);
  498. // *HACK: use the same texture repeats for all PBR terrain textures for now
  499. // (not compliant with KHR texture transform spec).
  500. shader->uniform4fv(LLShaderMgr::TEXTURE_BASE_COLOR_TRANSFORM, 2,
  501. (F32*)basecolor_packed);
  502. // Alpha ramp
  503. S32 ramp = sShader->enableTexture(LLViewerShaderMgr::TERRAIN_ALPHARAMP);
  504. LLTexUnit* ramp_unitp = gGL.getTexUnit(ramp);
  505. ramp_unitp->bind(m2DAlphaRampImagep);
  506. ramp_unitp->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  507. // GLTF uniforms
  508. static LLColor4 base_colors[MAT_COUNT];
  509. static LLColor3 emissive_colors[MAT_COUNT];
  510. static F32 metallic_factors[MAT_COUNT];
  511. static F32 roughness_factors[MAT_COUNT];
  512. static F32 minimum_alphas[MAT_COUNT];
  513. for (U32 i = 0; i < MAT_COUNT; ++i)
  514. {
  515. const LLGLTFMaterial* matp = materials[i];
  516. base_colors[i] = matp->mBaseColor;
  517. if (detail_mode >= TERRAIN_PBR_DETAIL_EMISSIVE)
  518. {
  519. emissive_colors[i] = matp->mEmissiveColor;
  520. }
  521. if (detail_mode >= TERRAIN_PBR_DETAIL_METALLIC_ROUGHNESS)
  522. {
  523. metallic_factors[i] = matp->mMetallicFactor;
  524. roughness_factors[i] = matp->mRoughnessFactor;
  525. }
  526. F32 min_alpha = 0.f;
  527. if (matp->mAlphaMode == LLGLTFMaterial::ALPHA_MODE_MASK)
  528. {
  529. // Dividing the alpha cutoff by transparency here allows the shader
  530. // to compare against the alpha value of the texture without
  531. // needing the transparency value.
  532. min_alpha = matp->mAlphaCutoff / matp->mBaseColor.mV[3];
  533. }
  534. minimum_alphas[i] = min_alpha;
  535. }
  536. shader->uniform4fv(LLShaderMgr::TERRAIN_BASE_COLOR_FACTORS, MAT_COUNT,
  537. (F32*)base_colors);
  538. if (detail_mode >= TERRAIN_PBR_DETAIL_METALLIC_ROUGHNESS)
  539. {
  540. shader->uniform4f(LLShaderMgr::TERRAIN_METALLIC_FACTORS,
  541. metallic_factors[0], metallic_factors[1],
  542. metallic_factors[2], metallic_factors[3]);
  543. shader->uniform4f(LLShaderMgr::TERRAIN_ROUGHNESS_FACTORS,
  544. roughness_factors[0], roughness_factors[1],
  545. roughness_factors[2], roughness_factors[3]);
  546. }
  547. if (detail_mode >= TERRAIN_PBR_DETAIL_EMISSIVE)
  548. {
  549. shader->uniform3fv(LLShaderMgr::TERRAIN_EMISSIVE_COLORS, MAT_COUNT,
  550. (F32*)emissive_colors);
  551. }
  552. shader->uniform4f(LLShaderMgr::TERRAIN_MINIMUM_ALPHAS, minimum_alphas[0],
  553. minimum_alphas[1], minimum_alphas[2], minimum_alphas[3]);
  554. // GL_BLEND disabled by default
  555. drawLoop();
  556. // Disable multitexture
  557. sShader->disableTexture(LLViewerShaderMgr::TERRAIN_ALPHARAMP);
  558. ramp_unitp->unbind(LLTexUnit::TT_TEXTURE);
  559. ramp_unitp->disable();
  560. ramp_unitp->activate();
  561. for (U32 i = 0; i < MAT_COUNT; ++i)
  562. {
  563. sShader->disableTexture(LLViewerShaderMgr::TERRAIN_DETAIL0_BASE_COLOR + i);
  564. if (detail_mode >= TERRAIN_PBR_DETAIL_NORMAL)
  565. {
  566. sShader->disableTexture(LLViewerShaderMgr::TERRAIN_DETAIL0_NORMAL + i);
  567. normal_unitp = gGL.getTexUnit(detail_normal[i]);
  568. }
  569. else
  570. {
  571. normal_unitp = NULL;
  572. }
  573. if (detail_mode >= TERRAIN_PBR_DETAIL_METALLIC_ROUGHNESS)
  574. {
  575. sShader->disableTexture(LLViewerShaderMgr::TERRAIN_DETAIL0_MROUGHNESS + i);
  576. mrough_unitp = gGL.getTexUnit(detail_mrough[i]);
  577. }
  578. else
  579. {
  580. mrough_unitp = NULL;
  581. }
  582. if (detail_mode >= TERRAIN_PBR_DETAIL_EMISSIVE)
  583. {
  584. sShader->disableTexture(LLViewerShaderMgr::TERRAIN_DETAIL0_EMISSIVE + i);
  585. emissive_unitp = gGL.getTexUnit(detail_emissive[i]);
  586. }
  587. else
  588. {
  589. emissive_unitp = NULL;
  590. }
  591. basecolor_unitp = gGL.getTexUnit(detail_basecolor[i]);
  592. basecolor_unitp->unbind(LLTexUnit::TT_TEXTURE);
  593. basecolor_unitp->disable();
  594. basecolor_unitp->activate();
  595. if (normal_unitp)
  596. {
  597. normal_unitp->unbind(LLTexUnit::TT_TEXTURE);
  598. normal_unitp->disable();
  599. normal_unitp->activate();
  600. }
  601. if (mrough_unitp)
  602. {
  603. mrough_unitp->unbind(LLTexUnit::TT_TEXTURE);
  604. mrough_unitp->disable();
  605. mrough_unitp->activate();
  606. }
  607. if (emissive_unitp)
  608. {
  609. emissive_unitp->unbind(LLTexUnit::TT_TEXTURE);
  610. emissive_unitp->disable();
  611. emissive_unitp->activate();
  612. }
  613. }
  614. }
  615. void LLDrawPoolTerrain::hilightParcelOwners()
  616. {
  617. if (gUsePBRShaders || mShaderLevel > 1)
  618. {
  619. // Use fullbright shader for highlighting
  620. LLGLSLShader* old_shaderp = sShader;
  621. sShader->unbind();
  622. sShader = gUsePBRShaders ? &gDeferredHighlightProgram
  623. : &gHighlightProgram;
  624. sShader->bind();
  625. gGL.diffuseColor4f(1.f, 1.f, 1.f, 1.f);
  626. LLGLEnable polyOffset(GL_POLYGON_OFFSET_FILL);
  627. glPolygonOffset(-1.f, -1.f);
  628. renderOwnership();
  629. sShader = old_shaderp;
  630. sShader->bind();
  631. }
  632. else
  633. {
  634. gPipeline.disableLights();
  635. renderOwnership();
  636. }
  637. }
  638. void LLDrawPoolTerrain::renderFull4TU()
  639. {
  640. //MK
  641. if (gRLenabled && gRLInterface.mContainsCamTextures)
  642. {
  643. renderSimple();
  644. return;
  645. }
  646. //mk
  647. LLViewerRegion* regionp =
  648. mDrawFace[0]->getDrawable()->getVObj()->getRegion();
  649. if (!regionp) return; // Paranoia
  650. static std::vector<LLViewerTexture*> textures;
  651. LLVLComposition* compp = regionp->getComposition();
  652. // *HACK: when the terrain is a PBR one, this will return the base color
  653. // textures, allowing to render something better than grey terrain, when
  654. // not in PBR rendering mode. HB
  655. compp->getTextures(textures);
  656. const F64 divisor = 1.0 / (F64)sDetailScale;
  657. LLVector3d region_origin_global = regionp->getOriginGlobal();
  658. F32 offset_x = (F32)fmod(region_origin_global.mdV[VX], divisor) *
  659. sDetailScale;
  660. F32 offset_y = (F32)fmod(region_origin_global.mdV[VY], divisor) *
  661. sDetailScale;
  662. LLVector4 tp0, tp1;
  663. tp0.set(sDetailScale, 0.f, 0.f, offset_x);
  664. tp1.set(0.f, sDetailScale, 0.f, offset_y);
  665. gGL.blendFunc(LLRender::BF_ONE_MINUS_SOURCE_ALPHA,
  666. LLRender::BF_SOURCE_ALPHA);
  667. //-------------------------------------------------------------------------
  668. // First pass
  669. //
  670. // Stage 0: detail texture 0
  671. //
  672. LLTexUnit* unit0 = gGL.getTexUnit(0);
  673. unit0->activate();
  674. unit0->bind(textures[0]);
  675. glEnable(GL_TEXTURE_GEN_S);
  676. glEnable(GL_TEXTURE_GEN_T);
  677. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  678. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  679. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  680. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  681. // Stage 1: generate alpha ramp for detail0/detail1 transition
  682. //
  683. LLTexUnit* unit1 = gGL.getTexUnit(1);
  684. unit1->bind(m2DAlphaRampImagep.get());
  685. unit1->enable(LLTexUnit::TT_TEXTURE);
  686. unit1->activate();
  687. //
  688. // Stage 2: Interpolate detail1 with existing based on ramp
  689. //
  690. LLTexUnit* unit2 = gGL.getTexUnit(2);
  691. unit2->bind(textures[1]);
  692. unit2->enable(LLTexUnit::TT_TEXTURE);
  693. unit2->activate();
  694. glEnable(GL_TEXTURE_GEN_S);
  695. glEnable(GL_TEXTURE_GEN_T);
  696. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  697. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  698. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  699. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  700. //
  701. // Stage 3: Modulate with primary (vertex) color for lighting
  702. //
  703. LLTexUnit* unit3 = gGL.getTexUnit(3);
  704. unit3->bind(textures[1]);
  705. unit3->enable(LLTexUnit::TT_TEXTURE);
  706. unit3->activate();
  707. unit0->activate();
  708. // GL_BLEND disabled by default
  709. drawLoop();
  710. //-------------------------------------------------------------------------
  711. // Second pass
  712. // Stage 0: write detail3 into base
  713. //
  714. unit0->activate();
  715. unit0->bind(textures[3]);
  716. glEnable(GL_TEXTURE_GEN_S);
  717. glEnable(GL_TEXTURE_GEN_T);
  718. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  719. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  720. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  721. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  722. //
  723. // Stage 1: generate alpha ramp for detail2/detail3 transition
  724. //
  725. unit1->bind(m2DAlphaRampImagep);
  726. unit1->enable(LLTexUnit::TT_TEXTURE);
  727. unit1->activate();
  728. // Set the texture matrix
  729. gGL.matrixMode(LLRender::MM_TEXTURE);
  730. gGL.loadIdentity();
  731. gGL.translatef(-2.f, 0.f, 0.f);
  732. //
  733. // Stage 2: Interpolate detail2 with existing based on ramp
  734. //
  735. unit2->bind(textures[2]);
  736. unit2->enable(LLTexUnit::TT_TEXTURE);
  737. unit2->activate();
  738. glEnable(GL_TEXTURE_GEN_S);
  739. glEnable(GL_TEXTURE_GEN_T);
  740. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  741. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  742. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  743. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  744. //
  745. // Stage 3: generate alpha ramp for detail1/detail2 transition
  746. //
  747. unit3->bind(m2DAlphaRampImagep);
  748. unit3->enable(LLTexUnit::TT_TEXTURE);
  749. unit3->activate();
  750. // Set the texture matrix
  751. gGL.matrixMode(LLRender::MM_TEXTURE);
  752. gGL.loadIdentity();
  753. gGL.translatef(-1.f, 0.f, 0.f);
  754. gGL.matrixMode(LLRender::MM_MODELVIEW);
  755. unit0->activate();
  756. {
  757. LLGLEnable blend(GL_BLEND);
  758. drawLoop();
  759. }
  760. LLVertexBuffer::unbind();
  761. // Disable multitexture
  762. unit3->unbind(LLTexUnit::TT_TEXTURE);
  763. unit3->disable();
  764. unit3->activate();
  765. gGL.matrixMode(LLRender::MM_TEXTURE);
  766. gGL.loadIdentity();
  767. gGL.matrixMode(LLRender::MM_MODELVIEW);
  768. unit2->unbind(LLTexUnit::TT_TEXTURE);
  769. unit2->disable();
  770. unit2->activate();
  771. glDisable(GL_TEXTURE_GEN_S);
  772. glDisable(GL_TEXTURE_GEN_T);
  773. gGL.matrixMode(LLRender::MM_TEXTURE);
  774. gGL.loadIdentity();
  775. gGL.matrixMode(LLRender::MM_MODELVIEW);
  776. unit1->unbind(LLTexUnit::TT_TEXTURE);
  777. unit1->disable();
  778. unit1->activate();
  779. gGL.matrixMode(LLRender::MM_TEXTURE);
  780. gGL.loadIdentity();
  781. gGL.matrixMode(LLRender::MM_MODELVIEW);
  782. // Restore blend state
  783. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  784. //-------------------------------------------------------------------------
  785. // Restore texture unit 0 defaults
  786. unit0->activate();
  787. unit0->unbind(LLTexUnit::TT_TEXTURE);
  788. glDisable(GL_TEXTURE_GEN_S);
  789. glDisable(GL_TEXTURE_GEN_T);
  790. gGL.matrixMode(LLRender::MM_TEXTURE);
  791. gGL.loadIdentity();
  792. gGL.matrixMode(LLRender::MM_MODELVIEW);
  793. }
  794. void LLDrawPoolTerrain::renderFull2TU()
  795. {
  796. //MK
  797. if (gRLenabled && gRLInterface.mContainsCamTextures)
  798. {
  799. renderSimple();
  800. return;
  801. }
  802. //mk
  803. LLViewerRegion* regionp =
  804. mDrawFace[0]->getDrawable()->getVObj()->getRegion();
  805. if (!regionp) return; // Paranoia
  806. static std::vector<LLViewerTexture*> textures;
  807. LLVLComposition* compp = regionp->getComposition();
  808. // *HACK: when the terrain is a PBR one, this will return the base color
  809. // textures, allowing to render something better than grey terrain, when
  810. // not in PBR rendering mode. HB
  811. compp->getTextures(textures);
  812. const F64 divisor = 1.0 / (F64)sDetailScale;
  813. LLVector3d region_origin_global = regionp->getOriginGlobal();
  814. F32 offset_x = (F32)fmod(region_origin_global.mdV[VX], divisor) *
  815. sDetailScale;
  816. F32 offset_y = (F32)fmod(region_origin_global.mdV[VY], divisor) *
  817. sDetailScale;
  818. LLVector4 tp0, tp1;
  819. tp0.set(sDetailScale, 0.f, 0.f, offset_x);
  820. tp1.set(0.f, sDetailScale, 0.f, offset_y);
  821. gGL.blendFunc(LLRender::BF_ONE_MINUS_SOURCE_ALPHA,
  822. LLRender::BF_SOURCE_ALPHA);
  823. //-------------------------------------------------------------------------
  824. // Pass 1/4
  825. //
  826. // Stage 0: render detail 0 into base
  827. //
  828. LLTexUnit* unit0 = gGL.getTexUnit(0);
  829. unit0->bind(textures[0]);
  830. glEnable(GL_TEXTURE_GEN_S);
  831. glEnable(GL_TEXTURE_GEN_T);
  832. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  833. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  834. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  835. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  836. drawLoop();
  837. //-------------------------------------------------------------------------
  838. // Pass 2/4
  839. //
  840. // Stage 0: generate alpha ramp for detail0/detail1 transition
  841. //
  842. unit0->bind(m2DAlphaRampImagep);
  843. glDisable(GL_TEXTURE_GEN_S);
  844. glDisable(GL_TEXTURE_GEN_T);
  845. //
  846. // Stage 1: write detail1
  847. //
  848. LLTexUnit* unit1 = gGL.getTexUnit(1);
  849. unit1->bind(textures[1]);
  850. unit1->enable(LLTexUnit::TT_TEXTURE);
  851. unit1->activate();
  852. glEnable(GL_TEXTURE_GEN_S);
  853. glEnable(GL_TEXTURE_GEN_T);
  854. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  855. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  856. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  857. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  858. unit0->activate();
  859. {
  860. LLGLEnable blend(GL_BLEND);
  861. drawLoop();
  862. }
  863. //-------------------------------------------------------------------------
  864. // Pass 3/4
  865. //
  866. // Stage 0: generate alpha ramp for detail1/detail2 transition
  867. //
  868. unit0->bind(m2DAlphaRampImagep);
  869. // Set the texture matrix
  870. gGL.matrixMode(LLRender::MM_TEXTURE);
  871. gGL.loadIdentity();
  872. gGL.translatef(-1.f, 0.f, 0.f);
  873. gGL.matrixMode(LLRender::MM_MODELVIEW);
  874. //
  875. // Stage 1: write detail2
  876. //
  877. unit1->bind(textures[2]);
  878. unit1->enable(LLTexUnit::TT_TEXTURE);
  879. unit1->activate();
  880. glEnable(GL_TEXTURE_GEN_S);
  881. glEnable(GL_TEXTURE_GEN_T);
  882. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  883. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  884. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  885. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  886. {
  887. LLGLEnable blend(GL_BLEND);
  888. drawLoop();
  889. }
  890. //-------------------------------------------------------------------------
  891. // Pass 4/4
  892. //
  893. // Stage 0: generate alpha ramp for detail2/detail3 transition
  894. //
  895. unit0->activate();
  896. unit0->bind(m2DAlphaRampImagep);
  897. // Set the texture matrix
  898. gGL.matrixMode(LLRender::MM_TEXTURE);
  899. gGL.loadIdentity();
  900. gGL.translatef(-2.f, 0.f, 0.f);
  901. gGL.matrixMode(LLRender::MM_MODELVIEW);
  902. // Stage 1: write detail3
  903. unit1->bind(textures[3]);
  904. unit1->enable(LLTexUnit::TT_TEXTURE);
  905. unit1->activate();
  906. glEnable(GL_TEXTURE_GEN_S);
  907. glEnable(GL_TEXTURE_GEN_T);
  908. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  909. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  910. glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0.mV);
  911. glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1.mV);
  912. unit0->activate();
  913. {
  914. LLGLEnable blend(GL_BLEND);
  915. drawLoop();
  916. }
  917. // Restore blend state
  918. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  919. // Disable multitexture
  920. unit1->unbind(LLTexUnit::TT_TEXTURE);
  921. unit1->disable();
  922. unit1->activate();
  923. glDisable(GL_TEXTURE_GEN_S);
  924. glDisable(GL_TEXTURE_GEN_T);
  925. gGL.matrixMode(LLRender::MM_TEXTURE);
  926. gGL.loadIdentity();
  927. gGL.matrixMode(LLRender::MM_MODELVIEW);
  928. //-------------------------------------------------------------------------
  929. // Restore texture unit 0 defaults
  930. unit0->activate();
  931. unit0->unbind(LLTexUnit::TT_TEXTURE);
  932. glDisable(GL_TEXTURE_GEN_S);
  933. glDisable(GL_TEXTURE_GEN_T);
  934. gGL.matrixMode(LLRender::MM_TEXTURE);
  935. gGL.loadIdentity();
  936. gGL.matrixMode(LLRender::MM_MODELVIEW);
  937. }
  938. void LLDrawPoolTerrain::renderSimple()
  939. {
  940. LLVector4 tp0, tp1;
  941. LLVector3 origin_agent =
  942. mDrawFace[0]->getDrawable()->getVObj()->getRegion()->getOriginAgent();
  943. const F32 tscale = 1.f / 256.f;
  944. tp0.set(tscale, 0.f, 0.f, origin_agent.mV[0] * -tscale);
  945. tp1.set(0.f, tscale, 0.f, origin_agent.mV[1] * -tscale);
  946. //-------------------------------------------------------------------------
  947. // Pass 1/1
  948. // Stage 0: base terrain texture pass
  949. if (mTexturep)
  950. {
  951. mTexturep->addTextureStats(1024.f * 1024.f);
  952. }
  953. LLTexUnit* unit0 = gGL.getTexUnit(0);
  954. unit0->activate();
  955. unit0->enable(LLTexUnit::TT_TEXTURE);
  956. //MK
  957. if (gRLenabled && gRLInterface.mContainsCamTextures &&
  958. gRLInterface.mCamTexturesCustom.notNull())
  959. {
  960. unit0->bind(gRLInterface.mCamTexturesCustom);
  961. }
  962. else
  963. //mk
  964. if (mTexturep)
  965. {
  966. unit0->bind(mTexturep);
  967. }
  968. sShader->uniform4fv(LLShaderMgr::OBJECT_PLANE_S, 1, tp0.mV);
  969. sShader->uniform4fv(LLShaderMgr::OBJECT_PLANE_T, 1, tp1.mV);
  970. drawLoop();
  971. //-------------------------------------------------------------------------
  972. // Restore texture unit 0 defaults
  973. unit0->activate();
  974. unit0->unbind(LLTexUnit::TT_TEXTURE);
  975. gGL.matrixMode(LLRender::MM_TEXTURE);
  976. gGL.loadIdentity();
  977. gGL.matrixMode(LLRender::MM_MODELVIEW);
  978. }
  979. void LLDrawPoolTerrain::renderOwnership()
  980. {
  981. LLGLSPipelineAlpha gls_pipeline_alpha;
  982. llassert(!mDrawFace.empty());
  983. // Each terrain pool is associated with a single region. We need to peek
  984. // back into the viewer's data to find out which ownership overlay texture
  985. // to use.
  986. LLFace* facep = mDrawFace[0];
  987. if (!facep) return; // Paranoia
  988. LLDrawable* drawablep = facep->getDrawable();
  989. if (!drawablep) return; // Paranoia
  990. const LLViewerObject* objectp = drawablep->getVObj();
  991. if (!objectp) return; // Paranoia
  992. const LLVOSurfacePatch* vo_surface_patchp = (LLVOSurfacePatch*)objectp;
  993. LLSurfacePatch* surface_patchp = vo_surface_patchp->getPatch();
  994. if (!surface_patchp) return; // Paranoia
  995. LLSurface* surfacep = surface_patchp->getSurface();
  996. if (!surfacep) return; // Paranoia
  997. LLViewerRegion* regionp = surfacep->getRegion();
  998. if (!regionp) return; // Paranoia
  999. LLViewerParcelOverlay* overlayp = regionp->getParcelOverlay();
  1000. if (!overlayp) return; // Paranoia
  1001. LLViewerTexture* texturep = overlayp->getTexture();
  1002. if (!texturep) return; // Paranoia
  1003. gGL.getTexUnit(0)->bind(texturep);
  1004. // *NOTE: because the region is 256 meters wide, but has 257 pixels, the
  1005. // texture coordinates for pixel 256x256 is not 1, 1. This makes the
  1006. // ownership map not line up with the selection. We address this with
  1007. // a texture matrix multiply.
  1008. gGL.matrixMode(LLRender::MM_TEXTURE);
  1009. gGL.pushMatrix();
  1010. constexpr F32 TEXTURE_FUDGE = 257.f / 256.f;
  1011. gGL.scalef(TEXTURE_FUDGE, TEXTURE_FUDGE, 1.f);
  1012. for (U32 i = 0, count = mDrawFace.size(); i < count; ++i)
  1013. {
  1014. LLFace* facep = mDrawFace[i];
  1015. if (facep) // Paranoia
  1016. {
  1017. // Note: mask is ignored for the PBR renderer
  1018. facep->renderIndexed(LLVertexBuffer::MAP_VERTEX |
  1019. LLVertexBuffer::MAP_TEXCOORD0);
  1020. }
  1021. }
  1022. gGL.matrixMode(LLRender::MM_TEXTURE);
  1023. gGL.popMatrix();
  1024. gGL.matrixMode(LLRender::MM_MODELVIEW);
  1025. }
  1026. void LLDrawPoolTerrain::dirtyTextures(const LLViewerTextureList::dirty_list_t& textures)
  1027. {
  1028. LLViewerFetchedTexture* texp =
  1029. LLViewerTextureManager::staticCast(mTexturep);
  1030. if (texp && textures.count(texp))
  1031. {
  1032. for (U32 i = 0, count = mReferences.size(); i < count; ++i)
  1033. {
  1034. LLFace* facep = mReferences[i];
  1035. if (facep) // Paranoia
  1036. {
  1037. gPipeline.markTextured(facep->getDrawable());
  1038. }
  1039. }
  1040. }
  1041. }
  1042. void LLDrawPoolTerrain::boostTerrainDetailTextures()
  1043. {
  1044. LLFace* facep = mDrawFace[0];
  1045. if (!facep) return; // Paranoia 1
  1046. LLDrawable* drawablep = mDrawFace[0]->getDrawable().get();
  1047. if (!drawablep) return; // Paranoia 2
  1048. LLViewerObject* objectp = drawablep->getVObj().get();
  1049. if (!objectp) return; // Paranoia 3
  1050. LLViewerRegion* regionp = objectp->getRegion();
  1051. if (!regionp) return; // Paranoia 4
  1052. LLVLComposition* compp = regionp->getComposition();
  1053. if (compp) // Paranoia 5
  1054. {
  1055. compp->boostTextures();
  1056. }
  1057. }
  1058. // Failed attempt at properly restoring terrain after GL restart with core GL
  1059. // profile enabled. HB
  1060. #if 0
  1061. void LLDrawPoolTerrain::rebuildPatches()
  1062. {
  1063. for (U32 i = 0, count = mDrawFace.size(); i < count; ++i)
  1064. {
  1065. LLFace* facep = mDrawFace[i];
  1066. if (!facep) continue; // Paranoia
  1067. LLDrawable* drawablep = facep->getDrawable();
  1068. if (drawablep) // Paranoia
  1069. {
  1070. gPipeline.markRebuild(drawablep);
  1071. LLViewerObject* objectp = drawablep->getVObj();
  1072. gPipeline.markGLRebuild(objectp);
  1073. LL_DEBUGS("MarkGLRebuild") << "Marked for GL rebuild: " << std::hex
  1074. << (intptr_t)objectp << std::dec
  1075. << LL_ENDL;
  1076. }
  1077. }
  1078. }
  1079. #endif