lldrawpoolwlsky.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /**
  2. * @file lldrawpoolwlsky.cpp
  3. * @brief LLDrawPoolWLSky class implementation
  4. *
  5. * $LicenseInfo:firstyear=2007&license=viewergpl$
  6. *
  7. * Copyright (c) 2007-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. #if LL_WINDOWS
  34. # pragma warning (push)
  35. # pragma warning (disable : 4702) // "Unreachable code"
  36. #endif
  37. #include "zlib.h"
  38. #define TINYEXR_USE_MINIZ 0
  39. #define TINYEXR_IMPLEMENTATION
  40. #include "tinyexr/tinyexr.h"
  41. #if LL_WINDOWS
  42. # pragma warning (pop)
  43. #endif
  44. #include "lldrawpoolwlsky.h"
  45. #include "llfasttimer.h"
  46. #include "llimage.h"
  47. #include "llappviewer.h" // For gFrameTimeSeconds
  48. #include "llenvironment.h"
  49. #include "llenvsettings.h"
  50. #include "llface.h"
  51. #include "llpipeline.h"
  52. #include "llsky.h"
  53. #include "llviewercamera.h"
  54. #include "llviewercontrol.h"
  55. #include "llviewerdisplay.h" // For gCubeSnapshot
  56. #include "llviewerregion.h"
  57. #include "llviewershadermgr.h"
  58. #include "llvowlsky.h"
  59. // Uniform names
  60. static LLStaticHashedString sCustomAlphaName("custom_alpha");
  61. static LLStaticHashedString sCamPosLocalName("camPosLocal");
  62. static LLStaticHashedString sHdriSplitScreen("hdri_split_screen");
  63. // Static member variables
  64. LLGLSLShader* LLDrawPoolWLSky::sCloudShader = NULL;
  65. LLGLSLShader* LLDrawPoolWLSky::sSkyShader = NULL;
  66. LLGLSLShader* LLDrawPoolWLSky::sSunShader = NULL;
  67. LLGLSLShader* LLDrawPoolWLSky::sMoonShader = NULL;
  68. bool LLDrawPoolWLSky::sUseHDRI = false;
  69. ///////////////////////////////////////////////////////////////////////////////
  70. // HDRI sky preview stuff
  71. LLPointer<LLImageGL> LLDrawPoolWLSky::sEXRImage;
  72. //static
  73. std::string LLDrawPoolWLSky::loadHDRISky(const std::string& filename)
  74. {
  75. float* out; // width * height * RGBA
  76. int width, height;
  77. const char* err = NULL;
  78. if (LoadEXRWithLayer(&out, &width, &height, filename.c_str(), NULL,
  79. &err) != TINYEXR_SUCCESS)
  80. {
  81. std::string error_msg = "Unknown";
  82. if (err)
  83. {
  84. error_msg.assign(err);
  85. FreeEXRErrorMessage(err);
  86. }
  87. return error_msg;
  88. }
  89. // Reset reflection maps when previewing a new HDRI
  90. gPipeline.mReflectionMapManager.reset();
  91. gPipeline.mReflectionMapManager.initReflectionMaps();
  92. U32 tex_name = 0;
  93. LLImageGL::generateTextures(1, &tex_name);
  94. sEXRImage = new LLImageGL(tex_name, 4, GL_TEXTURE_2D, GL_RGB16F, GL_RGB16F,
  95. GL_FLOAT, LLTexUnit::TAM_CLAMP);
  96. sEXRImage->setHasMipMaps(true);
  97. sEXRImage->setUseMipMaps(true);
  98. sEXRImage->setFilteringOption(LLTexUnit::TFO_TRILINEAR);
  99. LLTexUnit* unit0 = gGL.getTexUnit(0);
  100. unit0->bind(sEXRImage);
  101. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGBA,
  102. GL_FLOAT, out);
  103. free(out); // Release memory of image data
  104. glGenerateMipmap(GL_TEXTURE_2D);
  105. unit0->unbind(LLTexUnit::TT_TEXTURE);
  106. return "";
  107. }
  108. //static
  109. LLImageGL* LLDrawPoolWLSky::getHDRISky()
  110. {
  111. return sEXRImage.get();
  112. }
  113. //static
  114. void LLDrawPoolWLSky::resetHDRISky()
  115. {
  116. sEXRImage = NULL;
  117. }
  118. ///////////////////////////////////////////////////////////////////////////////
  119. LLDrawPoolWLSky::LLDrawPoolWLSky()
  120. : LLDrawPool(POOL_WL_SKY),
  121. mCamHeightLocal(0.f)
  122. {
  123. restoreGL();
  124. }
  125. // In this method, which is invoked before each set of render passes, we check
  126. // if we need to use the HDRI sky texture and cache the result in sUseHDRI for
  127. // speed (which avoids to have and check it at each render pass). HB
  128. //virtual
  129. void LLDrawPoolWLSky::prerender()
  130. {
  131. static LLCachedControl<bool> use_hdri(gSavedSettings, "RenderHDRIEnabled");
  132. if (!use_hdri || !gUsePBRShaders || sEXRImage.isNull())
  133. {
  134. sUseHDRI = false;
  135. return;
  136. }
  137. // Always use HDRI for reflection probes when available.
  138. static LLCachedControl<bool> irr_only(gSavedSettings,
  139. "RenderHDRIIrradianceOnly");
  140. if (gCubeSnapshot &&
  141. (!irr_only || !gPipeline.mReflectionMapManager.isRadiancePass()))
  142. {
  143. sUseHDRI = true;
  144. return;
  145. }
  146. // Fallback to EE sky when split screen is zero.
  147. static LLCachedControl<F32> split(gSavedSettings, "RenderHDRISplitScreen");
  148. sUseHDRI = split > 0.f;
  149. }
  150. // For EE rendering only
  151. //virtual
  152. void LLDrawPoolWLSky::beginRenderPass(S32)
  153. {
  154. if (LLPipeline::sUnderWaterRender)
  155. {
  156. sSkyShader = sCloudShader = &gObjectFullbrightNoColorWaterProgram;
  157. sSunShader = sMoonShader = &gObjectFullbrightNoColorWaterProgram;
  158. }
  159. else
  160. {
  161. sSkyShader = &gWLSkyProgram;
  162. sCloudShader = &gWLCloudProgram;
  163. sSunShader = &gWLSunProgram;
  164. sMoonShader = &gWLMoonProgram;
  165. }
  166. mCurrentSky = gEnvironment.getCurrentSky();
  167. #if LL_VARIABLE_SKY_DOME_SIZE
  168. mCamHeightLocal = envp->getCamHeight();
  169. #else
  170. mCamHeightLocal = SKY_DOME_OFFSET * SKY_DOME_RADIUS;
  171. #endif
  172. mCameraOrigin = gViewerCamera.getOrigin();
  173. }
  174. // For EE rendering only
  175. //virtual
  176. void LLDrawPoolWLSky::endRenderPass(S32)
  177. {
  178. sSkyShader = sCloudShader = sSunShader = sMoonShader = NULL;
  179. mCurrentSky = NULL;
  180. }
  181. // For EE rendering only
  182. //virtual
  183. void LLDrawPoolWLSky::render(S32 pass)
  184. {
  185. if (!gSky.mVOSkyp ||
  186. !gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_SKY))
  187. {
  188. return;
  189. }
  190. LL_FAST_TIMER(FTM_RENDER_WL_SKY);
  191. if (!mCurrentSky) // Paranoia
  192. {
  193. return;
  194. }
  195. renderSkyHaze();
  196. renderHeavenlyBodies();
  197. renderStars();
  198. renderSkyClouds();
  199. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  200. }
  201. //virtual
  202. void LLDrawPoolWLSky::beginDeferredPass(S32)
  203. {
  204. sSkyShader = &gDeferredWLSkyProgram;
  205. sCloudShader = &gDeferredWLCloudProgram;
  206. if (!gUsePBRShaders && LLPipeline::sUnderWaterRender)
  207. {
  208. sSunShader = sMoonShader = &gObjectFullbrightNoColorWaterProgram;
  209. }
  210. else
  211. {
  212. sSunShader = &gDeferredWLSunProgram;
  213. sMoonShader = &gDeferredWLMoonProgram;
  214. }
  215. mCurrentSky = gEnvironment.getCurrentSky();
  216. #if LL_VARIABLE_SKY_DOME_SIZE
  217. mCamHeightLocal = envp->getCamHeight();
  218. #else
  219. mCamHeightLocal = SKY_DOME_OFFSET * SKY_DOME_RADIUS;
  220. #endif
  221. mCameraOrigin = gViewerCamera.getOrigin();
  222. }
  223. //virtual
  224. void LLDrawPoolWLSky::endDeferredPass(S32)
  225. {
  226. sSkyShader = sCloudShader = sSunShader = sMoonShader = NULL;
  227. mCurrentSky = NULL;
  228. if (gUsePBRShaders)
  229. {
  230. // Clear the depth buffer so that haze shaders can use unwritten depth
  231. // as a mask.
  232. glClear(GL_DEPTH_BUFFER_BIT);
  233. }
  234. }
  235. //virtual
  236. void LLDrawPoolWLSky::renderDeferred(S32 pass)
  237. {
  238. if (!gSky.mVOSkyp ||
  239. !gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_SKY))
  240. {
  241. return;
  242. }
  243. LL_FAST_TIMER(FTM_RENDER_WL_SKY);
  244. if (!gPipeline.canUseWindLightShaders())
  245. {
  246. return;
  247. }
  248. if (!mCurrentSky) // Paranoia
  249. {
  250. return;
  251. }
  252. if (gUsePBRShaders && !gCubeSnapshot)
  253. {
  254. gSky.mVOSkyp->updateGeometry(gSky.mVOSkyp->mDrawable);
  255. }
  256. if (!gUsePBRShaders)
  257. {
  258. gGL.setColorMask(true, false);
  259. }
  260. renderSkyHazeDeferred();
  261. renderHeavenlyBodies();
  262. if (!gCubeSnapshot)
  263. {
  264. renderStarsDeferred();
  265. }
  266. if (!gCubeSnapshot ||
  267. // Do not draw clouds in irradiance maps to avoid popping
  268. gPipeline.mReflectionMapManager.isRadiancePass())
  269. {
  270. renderSkyClouds();
  271. }
  272. if (!gUsePBRShaders)
  273. {
  274. gGL.setColorMask(true, true);
  275. }
  276. }
  277. void LLDrawPoolWLSky::renderDome(LLGLSLShader* shaderp) const
  278. {
  279. gGL.matrixMode(LLRender::MM_MODELVIEW);
  280. gGL.pushMatrix();
  281. // Chop off translation
  282. if (LLPipeline::sReflectionRender && mCameraOrigin.mV[2] > 256.f)
  283. {
  284. gGL.translatef(mCameraOrigin.mV[0], mCameraOrigin.mV[1],
  285. 256.f - mCameraOrigin.mV[2] * 0.5f);
  286. }
  287. else
  288. {
  289. gGL.translatef(mCameraOrigin.mV[0], mCameraOrigin.mV[1],
  290. mCameraOrigin.mV[2]);
  291. }
  292. // The Windlight sky dome works most conveniently in a coordinate system
  293. // where Y is up, so permute our basis vectors accordingly.
  294. constexpr F32 SQRT3INV = 1.f / F_SQRT3;
  295. static const LLMatrix4a rot = gl_gen_rot(120.f, SQRT3INV, SQRT3INV,
  296. SQRT3INV);
  297. gGL.rotatef(rot);
  298. gGL.scalef(0.333f, 0.333f, 0.333f);
  299. gGL.translatef(0.f, -mCamHeightLocal, 0.f);
  300. // Draw WL Sky
  301. shaderp->uniform3f(sCamPosLocalName, 0.f, mCamHeightLocal, 0.f);
  302. gSky.mVOWLSkyp->drawDome();
  303. gGL.matrixMode(LLRender::MM_MODELVIEW);
  304. gGL.popMatrix();
  305. }
  306. // For EE rendering only
  307. void LLDrawPoolWLSky::renderSkyHaze() const
  308. {
  309. if (gPipeline.canUseWindLightShaders() &&
  310. gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_SKY))
  311. {
  312. LLGLSPipelineDepthTestSkyBox sky(GL_TRUE, GL_FALSE);
  313. sSkyShader->bind();
  314. sSkyShader->uniform1i(LLShaderMgr::SUN_UP_FACTOR, 1);
  315. sSkyShader->uniform1f(LLShaderMgr::SUN_MOON_GLOW_FACTOR,
  316. mCurrentSky->getSunMoonGlowFactor());
  317. // Render the skydome
  318. renderDome(sSkyShader);
  319. sSkyShader->unbind();
  320. }
  321. }
  322. void LLDrawPoolWLSky::renderSkyHazeDeferred() const
  323. {
  324. if (!gPipeline.canUseWindLightShaders() ||
  325. !gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_SKY))
  326. {
  327. return;
  328. }
  329. if (sUseHDRI)
  330. {
  331. sSkyShader = &gEnvironmentMapProgram;
  332. }
  333. sSkyShader->bind();
  334. if (sUseHDRI)
  335. {
  336. S32 chan = sSkyShader->enableTexture(LLShaderMgr::ENVIRONMENT_MAP);
  337. if (chan > -1)
  338. {
  339. gGL.getTexUnit(chan)->bind(sEXRImage);
  340. }
  341. static LLCachedControl<F32> hdri_exp(gSavedSettings,
  342. "RenderHDRIExposure");
  343. static LLCachedControl<F32> hdri_rot(gSavedSettings,
  344. "RenderHDRIRotation");
  345. static LLCachedControl<F32> hdri_split(gSavedSettings,
  346. "RenderHDRISplitScreen");
  347. sSkyShader->uniform1f(LLShaderMgr::SKY_HDR_SCALE, powf(2.f, hdri_exp));
  348. LLMatrix3 rot;
  349. rot.setRot(0.f, hdri_rot * DEG_TO_RAD, 0.f);
  350. sSkyShader->uniformMatrix3fv(LLShaderMgr::DEFERRED_ENV_MAT,
  351. 1, GL_FALSE, (F32*)rot.mMatrix);
  352. sSkyShader->uniform1f(sHdriSplitScreen,
  353. gCubeSnapshot ? 1.f : hdri_split);
  354. }
  355. LLGLSPipelineDepthTestSkyBox sky(GL_TRUE, GL_TRUE);
  356. if (gUsePBRShaders)
  357. {
  358. sSkyShader->uniform1i(LLShaderMgr::CUBE_SNAPSHOT,
  359. gCubeSnapshot ? 1 : 0);
  360. }
  361. sSkyShader->bindTexture(LLShaderMgr::RAINBOW_MAP,
  362. gSky.mVOSkyp->getRainbowTex());
  363. sSkyShader->bindTexture(LLShaderMgr::HALO_MAP, gSky.mVOSkyp->getHaloTex());
  364. sSkyShader->uniform1f(LLShaderMgr::ICE_LEVEL,
  365. mCurrentSky->getSkyIceLevel());
  366. bool sun_up = gPipeline.mIsSunUp;
  367. sSkyShader->uniform1i(LLShaderMgr::SUN_UP_FACTOR, sun_up ? 1 : 0);
  368. F32 moisture_level, droplet_radius;
  369. if (sun_up || gPipeline.mIsMoonUp)
  370. {
  371. moisture_level = mCurrentSky->getSkyMoistureLevel();
  372. droplet_radius = mCurrentSky->getSkyDropletRadius();
  373. }
  374. else
  375. {
  376. // Hobble halos and rainbows when there is no light source to generate
  377. // them
  378. moisture_level = droplet_radius = 0.f;
  379. }
  380. sSkyShader->uniform1f(LLShaderMgr::MOISTURE_LEVEL, moisture_level);
  381. sSkyShader->uniform1f(LLShaderMgr::DROPLET_RADIUS, droplet_radius);
  382. sSkyShader->uniform1f(LLShaderMgr::SUN_MOON_GLOW_FACTOR,
  383. mCurrentSky->getSunMoonGlowFactor());
  384. // Render the skydome
  385. renderDome(sSkyShader);
  386. sSkyShader->unbind();
  387. }
  388. void LLDrawPoolWLSky::renderSkyClouds() const
  389. {
  390. if (sUseHDRI || !gPipeline.canUseWindLightShaders() ||
  391. !gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_CLOUDS))
  392. {
  393. return;
  394. }
  395. LLViewerTexture* cloud_noise = gSky.mVOSkyp->getCloudNoiseTex();
  396. LLViewerTexture* cloud_noise_next = gSky.mVOSkyp->getCloudNoiseTexNext();
  397. if (!cloud_noise && !cloud_noise_next)
  398. {
  399. return;
  400. }
  401. LLGLSPipelineBlendSkyBox pipeline(GL_TRUE, GL_TRUE);
  402. sCloudShader->bind();
  403. LLTexUnit* unit0 = gGL.getTexUnit(0);
  404. LLTexUnit* unit1 = gGL.getTexUnit(1);
  405. unit0->unbind(LLTexUnit::TT_TEXTURE);
  406. unit1->unbind(LLTexUnit::TT_TEXTURE);
  407. F32 blend_factor = mCurrentSky->getBlendFactor();
  408. if (mCurrentSky->getCloudScrollRate().isExactlyZero())
  409. {
  410. blend_factor = 0.f;
  411. }
  412. if (cloud_noise && (!cloud_noise_next || cloud_noise == cloud_noise_next))
  413. {
  414. sCloudShader->bindTexture(LLShaderMgr::CLOUD_NOISE_MAP, cloud_noise,
  415. LLTexUnit::TT_TEXTURE);
  416. blend_factor = 0.f;
  417. }
  418. else if (cloud_noise_next && !cloud_noise)
  419. {
  420. sCloudShader->bindTexture(LLShaderMgr::CLOUD_NOISE_MAP,
  421. cloud_noise_next, LLTexUnit::TT_TEXTURE);
  422. blend_factor = 0.f;
  423. }
  424. else if (cloud_noise_next != cloud_noise)
  425. {
  426. sCloudShader->bindTexture(LLShaderMgr::CLOUD_NOISE_MAP, cloud_noise,
  427. LLTexUnit::TT_TEXTURE);
  428. sCloudShader->bindTexture(LLShaderMgr::CLOUD_NOISE_MAP_NEXT,
  429. cloud_noise_next, LLTexUnit::TT_TEXTURE);
  430. }
  431. sCloudShader->uniform1f(LLShaderMgr::BLEND_FACTOR, blend_factor);
  432. sCloudShader->uniform1f(LLShaderMgr::CLOUD_VARIANCE,
  433. mCurrentSky->getCloudVariance());
  434. sCloudShader->uniform1f(LLShaderMgr::SUN_MOON_GLOW_FACTOR,
  435. mCurrentSky->getSunMoonGlowFactor());
  436. // Render the skydome
  437. renderDome(sCloudShader);
  438. sCloudShader->unbind();
  439. unit0->unbind(LLTexUnit::TT_TEXTURE);
  440. unit1->unbind(LLTexUnit::TT_TEXTURE);
  441. }
  442. // For EE rendering only
  443. void LLDrawPoolWLSky::renderStars() const
  444. {
  445. F32 alpha = llclamp(mCurrentSky->getStarBrightness() / 512.f, 0.f, 1.f);
  446. if (alpha < 0.01f)
  447. {
  448. // There is no point in rendering almost invisible stars...
  449. return;
  450. }
  451. LLGLSPipelineBlendSkyBox gls_skybox(GL_TRUE, GL_FALSE);
  452. LLTexUnit* unit0 = gGL.getTexUnit(0);
  453. // *NOTE: have to have bound the cloud noise texture already since register
  454. // combiners blending below requires something to be bound and we might as
  455. // well only bind once.
  456. unit0->enable(LLTexUnit::TT_TEXTURE);
  457. LLViewerTexture* tex_a = gSky.mVOSkyp->getBloomTex();
  458. LLViewerTexture* tex_b = gSky.mVOSkyp->getBloomTexNext();
  459. if (tex_a && (!tex_b || tex_a == tex_b))
  460. {
  461. unit0->bind(tex_a);
  462. }
  463. else if (tex_b && !tex_a)
  464. {
  465. unit0->bind(tex_b);
  466. }
  467. else
  468. {
  469. unit0->bind(tex_a);
  470. }
  471. gGL.pushMatrix();
  472. gGL.translatef(mCameraOrigin.mV[0], mCameraOrigin.mV[1],
  473. mCameraOrigin.mV[2]);
  474. gGL.rotatef(gFrameTimeSeconds * 0.01f, 0.f, 0.f, 1.f);
  475. gStarsProgram.bind();
  476. gStarsProgram.uniform1f(sCustomAlphaName, alpha);
  477. gSky.mVOWLSkyp->drawStars();
  478. unit0->unbind(LLTexUnit::TT_TEXTURE);
  479. gStarsProgram.unbind();
  480. gGL.popMatrix();
  481. }
  482. void LLDrawPoolWLSky::renderStarsDeferred() const
  483. {
  484. if (sUseHDRI)
  485. {
  486. return;
  487. }
  488. F32 star_alpha = mCurrentSky->getStarBrightness() / 512.f;
  489. if (star_alpha < 0.001f)
  490. {
  491. return; // Stars too dim, nothing to draw !
  492. }
  493. LLGLSPipelineBlendSkyBox gls_sky(GL_TRUE, GL_FALSE);
  494. gGL.setSceneBlendType(LLRender::BT_ADD_WITH_ALPHA);
  495. LLTexUnit* unit0 = gGL.getTexUnit(0);
  496. LLTexUnit* unit1 = gGL.getTexUnit(1);
  497. F32 blend_factor = mCurrentSky->getBlendFactor();
  498. LLViewerTexture* tex_a = gSky.mVOSkyp->getBloomTex();
  499. LLViewerTexture* tex_b = gSky.mVOSkyp->getBloomTexNext();
  500. if (tex_a && (!tex_b || tex_a == tex_b))
  501. {
  502. unit0->bind(tex_a);
  503. unit1->unbind(LLTexUnit::TT_TEXTURE);
  504. blend_factor = 0.f;
  505. }
  506. else if (tex_b && !tex_a)
  507. {
  508. unit0->bind(tex_b);
  509. unit1->unbind(LLTexUnit::TT_TEXTURE);
  510. blend_factor = 0.f;
  511. }
  512. else if (tex_b != tex_a)
  513. {
  514. unit0->bind(tex_a);
  515. unit1->bind(tex_b);
  516. }
  517. gGL.pushMatrix();
  518. gGL.translatef(mCameraOrigin.mV[0], mCameraOrigin.mV[1],
  519. mCameraOrigin.mV[2]);
  520. gGL.rotatef(gFrameTimeSeconds * 0.01f, 0.f, 0.f, 1.f);
  521. gDeferredStarProgram.bind();
  522. gDeferredStarProgram.uniform1f(LLShaderMgr::BLEND_FACTOR, blend_factor);
  523. if (LLPipeline::sReflectionRender)
  524. {
  525. star_alpha = 1.f;
  526. }
  527. gDeferredStarProgram.uniform1f(sCustomAlphaName, star_alpha);
  528. F32 start_time = (F32)LLFrameTimer::getElapsedSeconds() * 0.5f;
  529. gDeferredStarProgram.uniform1f(LLShaderMgr::WATER_TIME, start_time);
  530. gSky.mVOWLSkyp->drawStars();
  531. unit0->unbind(LLTexUnit::TT_TEXTURE);
  532. unit1->unbind(LLTexUnit::TT_TEXTURE);
  533. gDeferredStarProgram.unbind();
  534. gGL.popMatrix();
  535. }
  536. void LLDrawPoolWLSky::renderHeavenlyBodies()
  537. {
  538. if (sUseHDRI)
  539. {
  540. return;
  541. }
  542. // SL-14113 we need moon to write to depth to clip stars behind
  543. LLGLSPipelineBlendSkyBox gls_skybox(GL_TRUE, GL_TRUE);
  544. gGL.pushMatrix();
  545. gGL.translatef(mCameraOrigin.mV[0], mCameraOrigin.mV[1],
  546. mCameraOrigin.mV[2]);
  547. F32 blend_factor = mCurrentSky->getBlendFactor();
  548. LLTexUnit* unit0 = gGL.getTexUnit(0);
  549. LLTexUnit* unit1 = gGL.getTexUnit(1);
  550. LLFace* face = gSky.mVOSkyp->mFace[LLVOSky::FACE_SUN];
  551. if (gSky.mVOSkyp->getSun().getDraw() && face && face->getGeomCount())
  552. {
  553. unit0->unbind(LLTexUnit::TT_TEXTURE);
  554. unit1->unbind(LLTexUnit::TT_TEXTURE);
  555. LLViewerTexture* tex_a = face->getTexture(LLRender::DIFFUSE_MAP);
  556. LLViewerTexture* tex_b =
  557. face->getTexture(LLRender::ALTERNATE_DIFFUSE_MAP);
  558. if (tex_a || tex_b)
  559. {
  560. sSunShader->bind();
  561. if (tex_a && (!tex_b || tex_a == tex_b))
  562. {
  563. sSunShader->bindTexture(LLShaderMgr::DIFFUSE_MAP, tex_a,
  564. LLTexUnit::TT_TEXTURE);
  565. blend_factor = 0.f;
  566. }
  567. else if (tex_b && !tex_a)
  568. {
  569. sSunShader->bindTexture(LLShaderMgr::DIFFUSE_MAP, tex_b,
  570. LLTexUnit::TT_TEXTURE);
  571. blend_factor = 0.f;
  572. }
  573. else if (tex_b != tex_a)
  574. {
  575. sSunShader->bindTexture(LLShaderMgr::DIFFUSE_MAP,
  576. tex_a, LLTexUnit::TT_TEXTURE);
  577. sSunShader->bindTexture(LLShaderMgr::ALTERNATE_DIFFUSE_MAP,
  578. tex_b, LLTexUnit::TT_TEXTURE);
  579. }
  580. LLColor4 color(gSky.mVOSkyp->getSun().getInterpColor());
  581. sSunShader->uniform4fv(LLShaderMgr::DIFFUSE_COLOR, 1, color.mV);
  582. sSunShader->uniform1f(LLShaderMgr::BLEND_FACTOR, blend_factor);
  583. face->renderIndexed();
  584. unit0->unbind(LLTexUnit::TT_TEXTURE);
  585. unit1->unbind(LLTexUnit::TT_TEXTURE);
  586. sSunShader->unbind();
  587. }
  588. }
  589. face = gSky.mVOSkyp->mFace[LLVOSky::FACE_MOON];
  590. if (gSky.mVOSkyp->getMoon().getDraw() && face && face->getGeomCount() &&
  591. gPipeline.canUseWindLightShaders())
  592. {
  593. LLViewerTexture* tex_a = face->getTexture(LLRender::DIFFUSE_MAP);
  594. LLViewerTexture* tex_b =
  595. face->getTexture(LLRender::ALTERNATE_DIFFUSE_MAP);
  596. sMoonShader->bind();
  597. if (tex_a && (!tex_b || tex_a == tex_b))
  598. {
  599. sMoonShader->bindTexture(LLShaderMgr::DIFFUSE_MAP, tex_a,
  600. LLTexUnit::TT_TEXTURE);
  601. }
  602. else if (tex_b && !tex_a)
  603. {
  604. sMoonShader->bindTexture(LLShaderMgr::DIFFUSE_MAP, tex_b,
  605. LLTexUnit::TT_TEXTURE);
  606. }
  607. else if (tex_b != tex_a)
  608. {
  609. sMoonShader->bindTexture(LLShaderMgr::DIFFUSE_MAP, tex_a,
  610. LLTexUnit::TT_TEXTURE);
  611. #if 0
  612. sMoonShader->bindTexture(LLShaderMgr::ALTERNATE_DIFFUSE_MAP, tex_b,
  613. LLTexUnit::TT_TEXTURE);
  614. #endif
  615. }
  616. #if 0
  617. sMoonShader->uniform1f(LLShaderMgr::BLEND_FACTOR, blend_factor);
  618. #endif
  619. if (gUsePBRShaders)
  620. {
  621. sMoonShader->uniform1f(LLShaderMgr::MOON_BRIGHTNESS,
  622. mCurrentSky->getMoonBrightness());
  623. const LLColor3& color = gSky.mVOSkyp->getMoon().getColor();
  624. sMoonShader->uniform3fv(LLShaderMgr::MOONLIGHT_COLOR, 1, color.mV);
  625. }
  626. else
  627. {
  628. // Fix the insufficient Moon brightness in EE mode. HB
  629. static LLCachedControl<F32> moonb(gSavedSettings,
  630. "RenderMoonBrightnessFactor");
  631. F32 factor = llclamp((F32)moonb, 1.f, 6.f);
  632. sMoonShader->uniform1f(LLShaderMgr::MOON_BRIGHTNESS,
  633. factor * mCurrentSky->getMoonBrightness());
  634. LLColor4 color(gSky.mVOSkyp->getMoon().getColor());
  635. sMoonShader->uniform4fv(LLShaderMgr::MOONLIGHT_COLOR, 1, color.mV);
  636. }
  637. LLColor4 color(gSky.mVOSkyp->getMoon().getInterpColor());
  638. sMoonShader->uniform4fv(LLShaderMgr::DIFFUSE_COLOR, 1, color.mV);
  639. sMoonShader->uniform3fv(LLShaderMgr::DEFERRED_MOON_DIR, 1,
  640. mCurrentSky->getMoonDirection().mV);
  641. face->renderIndexed();
  642. unit0->unbind(LLTexUnit::TT_TEXTURE);
  643. unit1->unbind(LLTexUnit::TT_TEXTURE);
  644. sMoonShader->unbind();
  645. }
  646. gGL.popMatrix();
  647. }
  648. //static
  649. void LLDrawPoolWLSky::restoreGL()
  650. {
  651. // We likely need to rebuild our current sky geometry
  652. if (gSky.mVOWLSkyp.notNull())
  653. {
  654. gSky.mVOWLSkyp->updateGeometry(gSky.mVOWLSkyp->mDrawable);
  655. }
  656. }