lldrawpoolalpha.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /**
  2. * @file lldrawpoolalpha.cpp
  3. * @brief LLDrawPoolAlpha 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 "lldrawpoolalpha.h"
  34. #include "llcubemap.h"
  35. #include "llfasttimer.h"
  36. #include "llgl.h"
  37. #include "llmodel.h"
  38. #include "llrender.h"
  39. #include "lldrawable.h"
  40. #include "llenvironment.h"
  41. #include "llface.h"
  42. #include "llgltfscenemanager.h"
  43. #include "llpipeline.h"
  44. #include "llsky.h"
  45. #include "llspatialpartition.h"
  46. #include "llviewercamera.h"
  47. #include "llviewercontrol.h"
  48. #include "llviewerdisplay.h" // For gCubeSnapshot
  49. #include "llviewerobjectlist.h" // For debugging
  50. #include "llviewerregion.h"
  51. #include "llviewershadermgr.h"
  52. #include "llviewertexturelist.h" // For debugging
  53. constexpr F32 MINIMUM_ALPHA = 0.004f; // ~ 1/255
  54. constexpr F32 MINIMUM_IMPOSTOR_ALPHA = 0.1f;
  55. static LLStaticHashedString sWaterSign("waterSign");
  56. bool LLDrawPoolAlpha::sShowDebugAlpha = false;
  57. static bool sDeferredRender = false;
  58. // Helper functions
  59. // EE renderer variant
  60. static void prepare_alpha_shader(LLGLSLShader* shaderp, bool texture_gamma,
  61. bool deferred)
  62. {
  63. if (deferred)
  64. {
  65. gPipeline.bindDeferredShader(*shaderp);
  66. }
  67. else
  68. {
  69. shaderp->bind();
  70. }
  71. S32 no_atmo = 0;
  72. if (LLPipeline::sRenderingHUDs)
  73. {
  74. no_atmo = 1;
  75. }
  76. shaderp->uniform1i(LLShaderMgr::NO_ATMO, no_atmo);
  77. F32 gamma = 1.f / LLPipeline::RenderDeferredDisplayGamma;
  78. shaderp->uniform1f(LLShaderMgr::DISPLAY_GAMMA, gamma);
  79. if (texture_gamma)
  80. {
  81. shaderp->uniform1f(LLShaderMgr::TEXTURE_GAMMA, 2.2f);
  82. }
  83. if (LLPipeline::sImpostorRender)
  84. {
  85. shaderp->setMinimumAlpha(MINIMUM_IMPOSTOR_ALPHA);
  86. }
  87. else
  88. {
  89. shaderp->setMinimumAlpha(MINIMUM_ALPHA);
  90. }
  91. // Also prepare rigged variant
  92. if (shaderp->mRiggedVariant && shaderp->mRiggedVariant != shaderp)
  93. {
  94. prepare_alpha_shader(shaderp->mRiggedVariant, texture_gamma, deferred);
  95. }
  96. }
  97. // PBR renderer variant
  98. static void prepare_alpha_shader(LLGLSLShader* shaderp, bool texture_gamma,
  99. bool deferred, F32 water_sign)
  100. {
  101. // Does this deferred shader need environment uniforms set such as sun_dir,
  102. // etc. ? Note: we do not actually need a gbuffer since we are doing
  103. // forward rendering (for transparency) post deferred rendering.
  104. // *TODO: bindDeferredShader() probably should have the updating of the
  105. // environment uniforms factored out into updateShaderEnvironmentUniforms()
  106. // i.e. shaders/class1/deferred/alphaF.glsl.
  107. if (deferred)
  108. {
  109. shaderp->mCanBindFast = false;
  110. }
  111. shaderp->bind();
  112. F32 gamma = 1.f / LLPipeline::RenderDeferredDisplayGamma;
  113. shaderp->uniform1f(LLShaderMgr::DISPLAY_GAMMA, gamma);
  114. if (texture_gamma)
  115. {
  116. shaderp->uniform1f(LLShaderMgr::TEXTURE_GAMMA, 2.2f);
  117. }
  118. const LLVector4a* near_clip;
  119. if (LLPipeline::sRenderingHUDs)
  120. {
  121. // For HUD attachments, only the pre-water pass is executed and we
  122. // never want to clip anything.
  123. static const LLVector4a hud_near_clip(0.f, 0.f, -1.f, 0.f);
  124. water_sign = 1.f;
  125. near_clip = &hud_near_clip;
  126. }
  127. else
  128. {
  129. near_clip = &LLPipeline::sWaterPlane;
  130. }
  131. shaderp->uniform1f(sWaterSign, water_sign);
  132. shaderp->uniform4fv(LLShaderMgr::WATER_WATERPLANE, 1,
  133. near_clip->getF32ptr());
  134. if (LLPipeline::sImpostorRender)
  135. {
  136. shaderp->setMinimumAlpha(MINIMUM_IMPOSTOR_ALPHA);
  137. }
  138. else
  139. {
  140. shaderp->setMinimumAlpha(MINIMUM_ALPHA);
  141. }
  142. // Also prepare rigged variant
  143. if (shaderp->mRiggedVariant && shaderp->mRiggedVariant != shaderp)
  144. {
  145. prepare_alpha_shader(shaderp->mRiggedVariant, texture_gamma, deferred,
  146. water_sign);
  147. }
  148. }
  149. static void prepare_forward_shader(LLGLSLShader* shaderp, F32 minimum_alpha)
  150. {
  151. shaderp->bind();
  152. shaderp->setMinimumAlpha(minimum_alpha);
  153. S32 no_atmo = LLPipeline::sRenderingHUDs ? 1 : 0;
  154. shaderp->uniform1i(LLShaderMgr::NO_ATMO, no_atmo);
  155. // Also prepare rigged variant
  156. if (shaderp->mRiggedVariant && shaderp->mRiggedVariant != shaderp)
  157. {
  158. prepare_forward_shader(shaderp->mRiggedVariant, minimum_alpha);
  159. }
  160. }
  161. ///////////////////////////////////////////////////////////////////////////////
  162. // LLDrawPoolAlpha class
  163. ///////////////////////////////////////////////////////////////////////////////
  164. LLDrawPoolAlpha::LLDrawPoolAlpha(U32 type)
  165. : LLRenderPass(type),
  166. mTargetShader(NULL),
  167. mSimpleShader(NULL),
  168. mFullbrightShader(NULL),
  169. mEmissiveShader(NULL),
  170. mPBRShader(NULL),
  171. mPBREmissiveShader(NULL),
  172. mColorSFactor(LLRender::BF_UNDEF),
  173. mColorDFactor(LLRender::BF_UNDEF),
  174. mAlphaSFactor(LLRender::BF_UNDEF),
  175. mAlphaDFactor(LLRender::BF_UNDEF)
  176. {
  177. }
  178. //virtual
  179. void LLDrawPoolAlpha::prerender()
  180. {
  181. mShaderLevel =
  182. gViewerShaderMgrp->getShaderLevel(LLViewerShaderMgr::SHADER_OBJECT);
  183. }
  184. //virtual
  185. void LLDrawPoolAlpha::renderPostDeferred(S32 pass)
  186. {
  187. if (gUsePBRShaders)
  188. {
  189. renderPostDeferredPBR(pass);
  190. return;
  191. }
  192. sDeferredRender = true;
  193. // Prepare shaders
  194. bool impostors = LLPipeline::sImpostorRender;
  195. if (impostors)
  196. {
  197. mSimpleShader = &gDeferredAlphaImpostorProgram;
  198. mFullbrightShader = &gDeferredFullbrightProgram;
  199. mEmissiveShader = &gObjectEmissiveProgram;
  200. }
  201. else if (LLPipeline::sUnderWaterRender)
  202. {
  203. mSimpleShader = &gDeferredAlphaWaterProgram;
  204. mFullbrightShader = &gDeferredFullbrightWaterProgram;
  205. mEmissiveShader = &gObjectEmissiveWaterProgram;
  206. }
  207. else
  208. {
  209. mSimpleShader = &gDeferredAlphaProgram;
  210. mFullbrightShader = &gDeferredFullbrightProgram;
  211. mEmissiveShader = &gObjectEmissiveProgram;
  212. }
  213. prepare_alpha_shader(mEmissiveShader, true, false);
  214. prepare_alpha_shader(mFullbrightShader, true, false);
  215. // Prime simple shader (loads shadow relevant uniforms)
  216. prepare_alpha_shader(mSimpleShader, false, true);
  217. LLGLSLShader* shader_array = LLPipeline::sUnderWaterRender ?
  218. gDeferredMaterialWaterProgram : gDeferredMaterialProgram;
  219. for (U32 i = 0; i < LLMaterial::SHADER_COUNT; ++i)
  220. {
  221. prepare_alpha_shader(&shader_array[i], false, false);
  222. }
  223. // First pass, render rigged objects only and drawn to depth buffer
  224. forwardRender(true);
  225. // Second pass, regular forward alpha rendering
  226. forwardRender();
  227. // Final pass, render to depth for depth of field effects
  228. if (!impostors && LLPipeline::RenderDepthOfField)
  229. {
  230. // Update depth buffer sampler
  231. gPipeline.mRT->mScreen.flush();
  232. LLRenderTarget& depth_rt = gPipeline.mDeferredDepth;
  233. LLRenderTarget& dscr_rt = gPipeline.mRT->mDeferredScreen;
  234. depth_rt.copyContents(dscr_rt,
  235. 0, 0, dscr_rt.getWidth(), dscr_rt.getHeight(),
  236. 0, 0, depth_rt.getWidth(), depth_rt.getHeight(),
  237. GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  238. depth_rt.bindTarget();
  239. mSimpleShader = mFullbrightShader = &gObjectFullbrightAlphaMaskProgram;
  240. mSimpleShader->bind();
  241. mSimpleShader->setMinimumAlpha(0.33f);
  242. // Mask off color buffer writes as we are only writing to depth buffer
  243. gGL.setColorMask(false, false);
  244. constexpr U32 MIN_MASK = LLVertexBuffer::MAP_TEXTURE_INDEX |
  245. LLVertexBuffer::MAP_TANGENT |
  246. LLVertexBuffer::MAP_TEXCOORD1 |
  247. LLVertexBuffer::MAP_TEXCOORD2;
  248. // If the face is more than 90% transparent, then do not update the
  249. // depth buffer for Dof since we not want nearly invisible objects to
  250. // cause DoF effects. Thus the 'true' below.
  251. renderAlpha(getVertexDataMask() | MIN_MASK, true);
  252. depth_rt.flush();
  253. gPipeline.mRT->mScreen.bindTarget();
  254. gGL.setColorMask(true, false);
  255. }
  256. sDeferredRender = false;
  257. }
  258. // Branched version for the PBR renderer
  259. void LLDrawPoolAlpha::renderPostDeferredPBR(S32 pass)
  260. {
  261. bool is_pre_water = mType == LLDrawPool::POOL_ALPHA_PRE_WATER;
  262. if (is_pre_water && LLPipeline::isWaterClip())
  263. {
  264. // Do not render alpha objects on the other side of the water plane if
  265. // water is opaque.
  266. return;
  267. }
  268. F32 water_sign = is_pre_water ? -1.f : 1.f;
  269. if (LLPipeline::sUnderWaterRender)
  270. {
  271. water_sign = -water_sign;
  272. }
  273. // Prepare shaders
  274. mEmissiveShader = &gDeferredEmissiveProgram;
  275. prepare_alpha_shader(mEmissiveShader, true, false, water_sign);
  276. mPBREmissiveShader = &gPBRGlowProgram;
  277. prepare_alpha_shader(mPBREmissiveShader, true, false, water_sign);
  278. bool impostors = LLPipeline::sImpostorRender;
  279. bool huds = LLPipeline::sRenderingHUDs;
  280. if (impostors)
  281. {
  282. mFullbrightShader = &gDeferredFullbrightAlphaMaskProgram;
  283. mSimpleShader = &gDeferredAlphaImpostorProgram;
  284. mPBRShader = &gDeferredPBRAlphaProgram;
  285. }
  286. else if (huds)
  287. {
  288. mFullbrightShader = &gHUDFullbrightAlphaMaskAlphaProgram;
  289. mSimpleShader = &gHUDAlphaProgram;
  290. mPBRShader = &gHUDPBRAlphaProgram;
  291. }
  292. else
  293. {
  294. mFullbrightShader = &gDeferredFullbrightAlphaMaskAlphaProgram;
  295. mSimpleShader = &gDeferredAlphaProgram;
  296. mPBRShader = &gDeferredPBRAlphaProgram;
  297. }
  298. prepare_alpha_shader(mFullbrightShader, true, true, water_sign);
  299. prepare_alpha_shader(mSimpleShader, false, true, water_sign);
  300. LLGLSLShader* mat_shaderp = gDeferredMaterialProgram;
  301. for (U32 i = 0; i < LLMaterial::SHADER_COUNT * 2; ++i)
  302. {
  303. prepare_alpha_shader(&mat_shaderp[i], false, true, water_sign);
  304. }
  305. prepare_alpha_shader(mPBRShader, false, true, water_sign);
  306. // Explicitly unbind here so render loop does not make assumptions about
  307. // the last shader already being setup for rendering.
  308. LLGLSLShader::unbind();
  309. if (!huds)
  310. {
  311. // First pass, render rigged objects only and drawn to depth buffer
  312. forwardRender(true);
  313. }
  314. // Second pass, regular forward alpha rendering.
  315. forwardRender();
  316. // Final pass, render to depth for depth of field effects
  317. if (!huds && !impostors && LLPipeline::RenderDepthOfField &&
  318. !gCubeSnapshot && mType == LLDrawPool::POOL_ALPHA_POST_WATER)
  319. {
  320. // Update depth buffer sampler
  321. mSimpleShader = mFullbrightShader =
  322. &gDeferredFullbrightAlphaMaskProgram;
  323. mSimpleShader->bind();
  324. mSimpleShader->setMinimumAlpha(0.33f);
  325. // Mask off color buffer writes as we are only writing to depth buffer
  326. gGL.setColorMask(false, false);
  327. constexpr U32 MIN_MASK = LLVertexBuffer::MAP_TEXTURE_INDEX |
  328. LLVertexBuffer::MAP_TANGENT |
  329. LLVertexBuffer::MAP_TEXCOORD1 |
  330. LLVertexBuffer::MAP_TEXCOORD2;
  331. // If the face is more than 90% transparent, then do not update the
  332. // depth buffer for Dof since we not want nearly invisible objects to
  333. // cause DoF effects. Thus the 'true' below.
  334. renderAlpha(getVertexDataMask() | MIN_MASK, true);
  335. gGL.setColorMask(true, false);
  336. }
  337. }
  338. // Only for the EE renderer
  339. //virtual
  340. void LLDrawPoolAlpha::render(S32 pass)
  341. {
  342. F32 minimum_alpha = 0.f;
  343. if (LLPipeline::sImpostorRender)
  344. {
  345. minimum_alpha = 0.5f;
  346. mSimpleShader = &gObjectSimpleImpostorProgram;
  347. mFullbrightShader = &gObjectFullbrightProgram;
  348. mEmissiveShader = &gObjectEmissiveProgram;
  349. }
  350. else if (LLPipeline::sUnderWaterRender)
  351. {
  352. mSimpleShader = &gObjectSimpleWaterProgram;
  353. mFullbrightShader = &gObjectFullbrightWaterProgram;
  354. mEmissiveShader = &gObjectEmissiveWaterProgram;
  355. }
  356. else
  357. {
  358. mSimpleShader = &gObjectSimpleProgram;
  359. mFullbrightShader = &gObjectFullbrightProgram;
  360. mEmissiveShader = &gObjectEmissiveProgram;
  361. }
  362. prepare_forward_shader(mFullbrightShader, minimum_alpha);
  363. prepare_forward_shader(mSimpleShader, minimum_alpha);
  364. LLGLSLShader* shader_array = LLPipeline::sUnderWaterRender ?
  365. gDeferredMaterialWaterProgram : gDeferredMaterialProgram;
  366. for (U32 i = 0; i < LLMaterial::SHADER_COUNT; ++i)
  367. {
  368. prepare_alpha_shader(&shader_array[i], false, false);
  369. }
  370. // First pass, render rigged objects only and drawn to depth buffer
  371. forwardRender(true);
  372. // Second pass, non-rigged, no depth buffer writes
  373. forwardRender();
  374. }
  375. void LLDrawPoolAlpha::forwardRender(bool rigged)
  376. {
  377. gPipeline.enableLightsDynamic();
  378. LLGLSPipelineAlpha gls_pipeline_alpha;
  379. // Enable writing to alpha for emissive effects
  380. gGL.setColorMask(true, true);
  381. bool write_depth = rigged || LLPipeline::sImpostorRenderAlphaDepthPass;
  382. if (!write_depth && gUsePBRShaders)
  383. {
  384. // Needed for accurate water fog
  385. write_depth = mType == LLDrawPoolAlpha::POOL_ALPHA_PRE_WATER;
  386. }
  387. LLGLDepthTest depth(GL_TRUE, write_depth ? GL_TRUE : GL_FALSE);
  388. if (write_depth && gUsePBRShaders)
  389. {
  390. // Draw GLTF scenes to depth buffer
  391. gPipeline.bindDeferredShader(gDeferredPBRAlphaProgram);
  392. LLGLTFSceneManager::renderAlpha();
  393. }
  394. // Regular alpha blend
  395. mColorSFactor = LLRender::BF_SOURCE_ALPHA;
  396. mColorDFactor = LLRender::BF_ONE_MINUS_SOURCE_ALPHA;
  397. // Glow suppression
  398. mAlphaSFactor = LLRender::BF_ZERO;
  399. mAlphaDFactor = LLRender::BF_ONE_MINUS_SOURCE_ALPHA;
  400. gGL.blendFunc(mColorSFactor, mColorDFactor, mAlphaSFactor, mAlphaDFactor);
  401. constexpr U32 MIN_MASK = LLVertexBuffer::MAP_TEXTURE_INDEX |
  402. LLVertexBuffer::MAP_TANGENT |
  403. LLVertexBuffer::MAP_TEXCOORD1 |
  404. LLVertexBuffer::MAP_TEXCOORD2;
  405. renderAlpha(getVertexDataMask() | MIN_MASK, false, rigged);
  406. gGL.setColorMask(true, false);
  407. if (!rigged && sShowDebugAlpha)
  408. {
  409. // Render "highlight alpha" on final non-rigged pass.
  410. // *HACK: this call is protected by !rigged instead of alongside
  411. // forwardRender() so that renderDebugAlpha is executed while
  412. // gls_pipeline_alpha and depth GL state variables above are still in
  413. // scope.
  414. renderDebugAlpha();
  415. }
  416. }
  417. void LLDrawPoolAlpha::renderDebugAlpha()
  418. {
  419. gHighlightProgram.bind();
  420. gGL.getTexUnit(0)->bindFast(LLViewerFetchedTexture::sSmokeImagep);
  421. if (gUsePBRShaders)
  422. {
  423. // Changed alpha from 1.f to 0.8f to avoid opaque highlighted textures
  424. // and get something closer to highlights seen in EE mode. HB
  425. constexpr F32 alpha = 0.8f;
  426. // Highlight (semi) transparent faces
  427. gGL.diffuseColor4f(1.f, 0.f, 0.f, alpha);
  428. renderAlphaHighlight();
  429. pushUntexturedBatches(PASS_ALPHA_MASK);
  430. pushUntexturedBatches(PASS_ALPHA_INVISIBLE);
  431. // Highlight alpha masking textures in blue
  432. gGL.diffuseColor4f(0.f, 0.f, 1.f, alpha);
  433. pushUntexturedBatches(PASS_MATERIAL_ALPHA_MASK);
  434. pushUntexturedBatches(PASS_NORMMAP_MASK);
  435. pushUntexturedBatches(PASS_SPECMAP_MASK);
  436. pushUntexturedBatches(PASS_NORMSPEC_MASK);
  437. pushUntexturedBatches(PASS_FULLBRIGHT_ALPHA_MASK);
  438. pushUntexturedBatches(PASS_MAT_PBR_ALPHA_MASK);
  439. // Highlight invisible faces in green
  440. gGL.diffuseColor4f(0.f, 1.f, 0.f, alpha);
  441. pushUntexturedBatches(PASS_INVISIBLE);
  442. // Bind the rigged shadder variant
  443. gHighlightProgram.mRiggedVariant->bind();
  444. // Highlight (semi) transparent faces
  445. gGL.diffuseColor4f(1.f, 0.f, 0.f, alpha);
  446. pushRiggedBatches(PASS_ALPHA_MASK_RIGGED, false);
  447. pushRiggedBatches(PASS_ALPHA_INVISIBLE_RIGGED, false);
  448. // Highlight alpha masking textures in blue
  449. gGL.diffuseColor4f(0.f, 0.f, 1.f, alpha);
  450. pushRiggedBatches(PASS_MATERIAL_ALPHA_MASK_RIGGED, false);
  451. pushRiggedBatches(PASS_NORMMAP_MASK_RIGGED, false);
  452. pushRiggedBatches(PASS_SPECMAP_MASK_RIGGED, false);
  453. pushRiggedBatches(PASS_NORMSPEC_MASK_RIGGED, false);
  454. pushRiggedBatches(PASS_FULLBRIGHT_ALPHA_MASK_RIGGED, false);
  455. pushRiggedBatches(PASS_MAT_PBR_ALPHA_MASK_RIGGED, false);
  456. // Highlight invisible faces in green
  457. gGL.diffuseColor4f(0.f, 1.f, 0.f, alpha);
  458. pushRiggedBatches(PASS_INVISIBLE_RIGGED, false);
  459. LLGLSLShader::sCurBoundShaderPtr->unbind();
  460. return;
  461. }
  462. constexpr U32 mask = LLVertexBuffer::MAP_VERTEX |
  463. LLVertexBuffer::MAP_TEXCOORD0;
  464. // Highlight (semi) transparent faces
  465. gGL.diffuseColor4f(1.f, 0.f, 0.f, 1.f);
  466. renderAlphaHighlight(mask);
  467. pushBatches(PASS_ALPHA_MASK, mask, false);
  468. pushBatches(PASS_ALPHA_INVISIBLE, mask, false);
  469. pushBatches(PASS_FULLBRIGHT_ALPHA_MASK, mask, false);
  470. // Highlight invisible faces in green
  471. gGL.diffuseColor4f(0.f, 1.f, 0.f, 1.f);
  472. pushBatches(PASS_INVISIBLE, mask, false);
  473. if (LLPipeline::sRenderDeferred)
  474. {
  475. // Highlight alpha masking textures in blue when in deferred rendering
  476. // mode.
  477. gGL.diffuseColor4f(0.f, 0.f, 1.f, 1.f);
  478. pushBatches(PASS_MATERIAL_ALPHA_MASK, mask, false);
  479. pushBatches(PASS_NORMMAP_MASK, mask, false);
  480. pushBatches(PASS_SPECMAP_MASK, mask, false);
  481. pushBatches(PASS_NORMSPEC_MASK, mask, false);
  482. }
  483. // Rigged variants now...
  484. gHighlightProgram.mRiggedVariant->bind();
  485. // Highlight (semi) transparent faces
  486. gGL.diffuseColor4f(1.f, 0.f, 0.f, 1.f);
  487. pushRiggedBatches(PASS_ALPHA_MASK_RIGGED, mask, false);
  488. pushRiggedBatches(PASS_ALPHA_INVISIBLE_RIGGED, mask, false);
  489. pushRiggedBatches(PASS_FULLBRIGHT_ALPHA_MASK_RIGGED, mask, false);
  490. // Highlight invisible faces in green
  491. gGL.diffuseColor4f(0.f, 1.f, 0.f, 1.f);
  492. pushRiggedBatches(PASS_INVISIBLE_RIGGED, mask, false);
  493. if (LLPipeline::sRenderDeferred)
  494. {
  495. // Highlight alpha masking textures in blue when in deferred rendering
  496. // mode.
  497. gGL.diffuseColor4f(0.f, 0.f, 1.f, 1.f);
  498. pushRiggedBatches(PASS_MATERIAL_ALPHA_MASK_RIGGED, mask, false);
  499. pushRiggedBatches(PASS_NORMMAP_MASK_RIGGED, mask, false);
  500. pushRiggedBatches(PASS_SPECMAP_MASK_RIGGED, mask, false);
  501. pushRiggedBatches(PASS_NORMSPEC_MASK_RIGGED, mask, false);
  502. }
  503. LLGLSLShader::sCurBoundShaderPtr->unbind();
  504. }
  505. void LLDrawPoolAlpha::renderAlphaHighlight(U32 mask)
  506. {
  507. if (!gPipeline.sCull)
  508. {
  509. // Paranoia (sCull != NULL needed for get*AlphaGroups())
  510. return;
  511. }
  512. // Two passes: one not rigged and one rigged.
  513. for (U32 pass = 0; pass < 2; ++pass)
  514. {
  515. LLVOAvatar* last_avatarp = NULL;
  516. U64 last_hash = 0;
  517. LLCullResult::sg_list_t& alpha_groups =
  518. pass ? gPipeline.getRiggedAlphaGroups()
  519. : gPipeline.getAlphaGroups();
  520. for (U32 i = 0, acount = alpha_groups.size(); i < acount; ++i)
  521. {
  522. LLSpatialGroup* groupp = alpha_groups[i];
  523. if (!groupp || groupp->isDead()) continue;
  524. LLSpatialPartition* partp = groupp->getSpatialPartition();
  525. if (!partp || !partp->mRenderByGroup) continue;
  526. // Use 'pass' to point on PASS_ALPHA_RIGGED on second pass
  527. LLSpatialGroup::drawmap_elem_t& draw_info =
  528. groupp->mDrawMap[PASS_ALPHA + pass];
  529. for (U32 j = 0, dcount = draw_info.size(); j < dcount; ++j)
  530. {
  531. LLDrawInfo& params = *draw_info[j];
  532. if (!params.mVertexBuffer)
  533. {
  534. continue;
  535. }
  536. bool rigged = params.mAvatar.notNull();
  537. gHighlightProgram.bind(rigged);
  538. if (rigged && params.mSkinInfo.notNull() &&
  539. (params.mAvatar.get() != last_avatarp ||
  540. params.mSkinInfo->mHash != last_hash))
  541. {
  542. if (!uploadMatrixPalette(params))
  543. {
  544. continue;
  545. }
  546. last_avatarp = params.mAvatar.get();
  547. last_hash = params.mSkinInfo->mHash;
  548. }
  549. LLRenderPass::applyModelMatrix(params);
  550. // Note: mask is ignored for the PBR renderer
  551. params.mVertexBuffer->setBufferFast(mask);
  552. params.mVertexBuffer->drawRangeFast(params.mStart, params.mEnd,
  553. params.mCount,
  554. params.mOffset);
  555. }
  556. // Add weights to the mask for the second, rigged pass
  557. mask |= LLVertexBuffer::MAP_WEIGHT4;
  558. }
  559. }
  560. // Make sure static version of highlight shader is bound before returning
  561. gHighlightProgram.bind();
  562. }
  563. bool LLDrawPoolAlpha::texSetup(LLDrawInfo* drawp, bool use_material,
  564. LLTexUnit* unitp)
  565. {
  566. LL_TRACY_TIMER(TRC_RENDER_ALPHA_TEX_BINDS);
  567. if (gUsePBRShaders && drawp->mGLTFMaterial)
  568. {
  569. if (drawp->mTextureMatrix)
  570. {
  571. unitp->activate();
  572. gGL.matrixMode(LLRender::MM_TEXTURE);
  573. gGL.loadMatrix(drawp->mTextureMatrix->getF32ptr());
  574. ++gPipeline.mTextureMatrixOps;
  575. return true;
  576. }
  577. return false;
  578. }
  579. LLGLSLShader* shaderp = LLGLSLShader::sCurBoundShaderPtr;
  580. bool tex_setup = false;
  581. bool render_mat = use_material && shaderp;
  582. if (render_mat)
  583. {
  584. render_mat = gUsePBRShaders ? !LLPipeline::sRenderingHUDs
  585. : sDeferredRender;
  586. }
  587. if (render_mat)
  588. {
  589. LL_TRACY_TIMER(TRC_RENDER_ALPHA_DEFERRED_TEX_BINDS);
  590. if (drawp->mNormalMap)
  591. {
  592. drawp->mNormalMap->addTextureStats(drawp->mVSize);
  593. shaderp->bindTexture(LLShaderMgr::BUMP_MAP, drawp->mNormalMap);
  594. }
  595. if (drawp->mSpecularMap)
  596. {
  597. drawp->mSpecularMap->addTextureStats(drawp->mVSize);
  598. shaderp->bindTexture(LLShaderMgr::SPECULAR_MAP,
  599. drawp->mSpecularMap);
  600. }
  601. }
  602. else if (shaderp == mSimpleShader ||
  603. shaderp == mSimpleShader->mRiggedVariant)
  604. {
  605. shaderp->bindTexture(LLShaderMgr::BUMP_MAP,
  606. LLViewerFetchedTexture::sFlatNormalImagep);
  607. shaderp->bindTexture(LLShaderMgr::SPECULAR_MAP,
  608. LLViewerFetchedTexture::sWhiteImagep);
  609. }
  610. U32 count = drawp->mTextureList.size();
  611. if (count > LL_NUM_TEXTURE_LAYERS)
  612. {
  613. llwarns << "We have only " << LL_NUM_TEXTURE_LAYERS
  614. << " TexUnits and this batch contains " << count
  615. << " textures. Rendering will be incomplete !" << llendl;
  616. count = LL_NUM_TEXTURE_LAYERS;
  617. }
  618. if (count > 1)
  619. {
  620. for (U32 i = 0; i < count; ++i)
  621. {
  622. LLViewerTexture* texp = drawp->mTextureList[i].get();
  623. if (texp)
  624. {
  625. gGL.getTexUnit(i)->bindFast(texp);
  626. }
  627. }
  628. }
  629. // Not batching textures or batch has only 1 texture; we might need a
  630. // texture matrix.
  631. else if (drawp->mTexture.notNull())
  632. {
  633. if (use_material)
  634. {
  635. shaderp->bindTexture(LLShaderMgr::DIFFUSE_MAP, drawp->mTexture);
  636. }
  637. else
  638. {
  639. unitp->bindFast(drawp->mTexture);
  640. }
  641. if (drawp->mTextureMatrix)
  642. {
  643. tex_setup = true;
  644. unitp->activate();
  645. gGL.matrixMode(LLRender::MM_TEXTURE);
  646. gGL.loadMatrix(drawp->mTextureMatrix->getF32ptr());
  647. ++gPipeline.mTextureMatrixOps;
  648. }
  649. }
  650. else
  651. {
  652. unitp->unbindFast(LLTexUnit::TT_TEXTURE);
  653. }
  654. return tex_setup;
  655. }
  656. void LLDrawPoolAlpha::renderEmissives(U32 mask, const drawinfo_vec_t& ems)
  657. {
  658. if (!mEmissiveShader) return; // Paranoia
  659. mEmissiveShader->bind();
  660. mEmissiveShader->uniform1f(LLShaderMgr::EMISSIVE_BRIGHTNESS, 1.f);
  661. // Note: mask is ignored for the PBR renderer
  662. mask = (mask & ~LLVertexBuffer::MAP_COLOR) | LLVertexBuffer::MAP_EMISSIVE;
  663. LLTexUnit* unit0 = gGL.getTexUnit(0);
  664. for (U32 i = 0, count = ems.size(); i < count; ++i)
  665. {
  666. LLDrawInfo* drawp = ems[i];
  667. bool tex_setup = texSetup(drawp, false, unit0);
  668. drawp->mVertexBuffer->setBufferFast(mask);
  669. drawp->mVertexBuffer->drawRangeFast(drawp->mStart, drawp->mEnd,
  670. drawp->mCount, drawp->mOffset);
  671. // Restore tex setup
  672. if (tex_setup)
  673. {
  674. unit0->activate();
  675. // Note: activate() did change matrix mode to MM_TEXTURE, so the
  676. // loadIdentity() call does apply to MM_TEXTURE. HB
  677. gGL.loadIdentity();
  678. gGL.matrixMode(LLRender::MM_MODELVIEW);
  679. }
  680. }
  681. }
  682. // PBR rendering only
  683. void LLDrawPoolAlpha::renderPbrEmissives(const drawinfo_vec_t& ems)
  684. {
  685. if (!mPBREmissiveShader) return; // Paranoia
  686. mPBREmissiveShader->bind();
  687. for (U32 i = 0, count = ems.size(); i < count; ++i)
  688. {
  689. LLDrawInfo* drawp = ems[i];
  690. LLGLDisable cull_face(drawp->mGLTFMaterial->mDoubleSided ? GL_CULL_FACE
  691. : 0);
  692. drawp->mGLTFMaterial->bind(drawp->mTexture, drawp->mVSize);
  693. drawp->mVertexBuffer->setBuffer();
  694. drawp->mVertexBuffer->drawRange(LLRender::TRIANGLES, drawp->mStart,
  695. drawp->mEnd, drawp->mCount,
  696. drawp->mOffset);
  697. }
  698. }
  699. void LLDrawPoolAlpha::renderRiggedEmissives(U32 mask,
  700. const drawinfo_vec_t& ems)
  701. {
  702. LLGLSLShader* shaderp = mEmissiveShader->mRiggedVariant;
  703. if (!shaderp) return; // Paranoia
  704. // Disable depth writes since "emissive" is additive so sorting does not
  705. // matter
  706. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  707. shaderp->bind();
  708. shaderp->uniform1f(LLShaderMgr::EMISSIVE_BRIGHTNESS, 1.f);
  709. LLVOAvatar* last_avatarp = NULL;
  710. U64 last_hash = 0;
  711. // Note: mask is ignored for the PBR renderer
  712. mask = (mask & ~LLVertexBuffer::MAP_COLOR) | LLVertexBuffer::MAP_EMISSIVE |
  713. LLVertexBuffer::MAP_WEIGHT4;
  714. LLTexUnit* unit0 = gGL.getTexUnit(0);
  715. for (U32 i = 0, count = ems.size(); i < count; ++i)
  716. {
  717. LLDrawInfo* drawp = ems[i];
  718. bool tex_setup = texSetup(drawp, false, unit0);
  719. if (drawp->mAvatar.notNull() && drawp->mSkinInfo.notNull() &&
  720. (drawp->mAvatar.get() != last_avatarp ||
  721. drawp->mSkinInfo->mHash != last_hash))
  722. {
  723. if (!uploadMatrixPalette(*drawp))
  724. {
  725. continue;
  726. }
  727. last_avatarp = drawp->mAvatar.get();
  728. last_hash = drawp->mSkinInfo->mHash;
  729. }
  730. drawp->mVertexBuffer->setBufferFast(mask);
  731. drawp->mVertexBuffer->drawRangeFast(drawp->mStart, drawp->mEnd,
  732. drawp->mCount, drawp->mOffset);
  733. // Restore tex setup
  734. if (tex_setup)
  735. {
  736. unit0->activate();
  737. // Note: activate() did change matrix mode to MM_TEXTURE, so the
  738. // loadIdentity() call does apply to MM_TEXTURE. HB
  739. gGL.loadIdentity();
  740. gGL.matrixMode(LLRender::MM_MODELVIEW);
  741. }
  742. }
  743. }
  744. // PBR rendering only
  745. void LLDrawPoolAlpha::renderRiggedPbrEmissives(const drawinfo_vec_t& ems)
  746. {
  747. if (!mPBREmissiveShader) return; // Paranoia
  748. // Disable depth writes since "emissive" is additive so sorting does not
  749. // matter
  750. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  751. mPBREmissiveShader->bind();
  752. LLVOAvatar* last_avatarp = NULL;
  753. U64 last_hash = 0;
  754. for (U32 i = 0, count = ems.size(); i < count; ++i)
  755. {
  756. LLDrawInfo* drawp = ems[i];
  757. if (drawp->mAvatar.notNull() && drawp->mSkinInfo.notNull() &&
  758. (drawp->mAvatar.get() != last_avatarp ||
  759. drawp->mSkinInfo->mHash != last_hash))
  760. {
  761. if (!uploadMatrixPalette(*drawp))
  762. {
  763. continue;
  764. }
  765. last_avatarp = drawp->mAvatar.get();
  766. last_hash = drawp->mSkinInfo->mHash;
  767. }
  768. LLGLDisable cull_face(drawp->mGLTFMaterial->mDoubleSided ? GL_CULL_FACE
  769. : 0);
  770. drawp->mGLTFMaterial->bind(drawp->mTexture, drawp->mVSize);
  771. drawp->mVertexBuffer->setBuffer();
  772. drawp->mVertexBuffer->drawRange(LLRender::TRIANGLES, drawp->mStart,
  773. drawp->mEnd, drawp->mCount,
  774. drawp->mOffset);
  775. }
  776. }
  777. static bool check_vb_mask(U32 mask, U32 expected_mask)
  778. {
  779. U32 missing = expected_mask & ~mask;
  780. if (!missing)
  781. {
  782. return true;
  783. }
  784. if (gDebugGL)
  785. {
  786. llwarns << "Missing required components:"
  787. << LLVertexBuffer::listMissingBits(missing) << llendl;
  788. }
  789. static LLCachedControl<bool> ignore(gSavedSettings,
  790. "RenderIgnoreBadVBMask");
  791. return ignore;
  792. }
  793. void LLDrawPoolAlpha::renderAlpha(U32 mask, bool depth_only, bool rigged)
  794. {
  795. if (!gPipeline.sCull)
  796. {
  797. // Paranoia (sCull != NULL needed for get*AlphaGroups())
  798. return;
  799. }
  800. bool initialized_lighting = false;
  801. bool light_enabled = true;
  802. LLVOAvatar* last_avatarp = NULL;
  803. U64 last_hash = 0;
  804. LLGLSLShader* last_shaderp = NULL;
  805. LLTexUnit* unit0 = gGL.getTexUnit(0);
  806. static drawinfo_vec_t emissives, rigged_emissives;
  807. static drawinfo_vec_t pbr_emissives, pbr_rigged_emissives;
  808. // No shaders = no glow.
  809. #if 1
  810. bool draw_glow = gUsePBRShaders || (!depth_only && mShaderLevel > 0);
  811. #else
  812. bool draw_glow = mShaderLevel > 0;
  813. #endif
  814. F32 water_height = 0.f;
  815. bool above_water = mType == POOL_ALPHA_POST_WATER;
  816. bool check_water = gUsePBRShaders && !LLPipeline::sRenderingHUDs;
  817. if (check_water)
  818. {
  819. if (LLPipeline::sUnderWaterRender)
  820. {
  821. above_water = !above_water;
  822. }
  823. water_height = gPipeline.mWaterHeight;
  824. }
  825. bool is_pre_water = gUsePBRShaders && mType == POOL_ALPHA_PRE_WATER;
  826. bool underwater = LLPipeline::sUnderWaterRender && !gUsePBRShaders;
  827. LLCullResult::sg_list_t& alpha_groups =
  828. rigged ? gPipeline.getRiggedAlphaGroups() : gPipeline.getAlphaGroups();
  829. S32 map_idx = rigged ? PASS_ALPHA_RIGGED : PASS_ALPHA;
  830. for (U32 i = 0, acount = alpha_groups.size(); i < acount; ++i)
  831. {
  832. LLSpatialGroup* groupp = alpha_groups[i];
  833. if (!groupp || groupp->isDead())
  834. {
  835. continue;
  836. }
  837. LLSpatialPartition* partp = groupp->getSpatialPartition();
  838. if (!partp || !partp->mRenderByGroup)
  839. {
  840. llassert(partp);
  841. continue;
  842. }
  843. if (check_water)
  844. {
  845. LLSpatialBridge* bridgep = partp->asBridge();
  846. const LLVector4a* ext = bridgep ? bridgep->getSpatialExtents()
  847. : groupp->getExtents();
  848. if (above_water)
  849. {
  850. if (ext[1].getF32ptr()[2] < water_height)
  851. {
  852. // Reject spatial groups which have no part above water
  853. continue;
  854. }
  855. }
  856. else if (ext[0].getF32ptr()[2] > water_height)
  857. {
  858. // Reject spatial groups which have no part below water
  859. continue;
  860. }
  861. }
  862. emissives.clear();
  863. rigged_emissives.clear();
  864. pbr_emissives.clear();
  865. pbr_rigged_emissives.clear();
  866. U32 part_type = partp->mPartitionType;
  867. bool is_particle =
  868. part_type == LLViewerRegion::PARTITION_PARTICLE ||
  869. part_type == LLViewerRegion::PARTITION_HUD_PARTICLE ||
  870. part_type == LLViewerRegion::PARTITION_CLOUD;
  871. LLGLDisable cull(is_particle ? GL_CULL_FACE : 0);
  872. LLSpatialGroup::drawmap_elem_t& draw_info = groupp->mDrawMap[map_idx];
  873. for (U32 j = 0, dcount = draw_info.size(); j < dcount; ++j)
  874. {
  875. LLDrawInfo& params = *draw_info[j];
  876. if (bool(params.mAvatar) != rigged || !params.mVertexBuffer)
  877. {
  878. continue;
  879. }
  880. if (!gUsePBRShaders &&
  881. (!check_vb_mask(params.mVertexBuffer->getTypeMask(), mask)))
  882. {
  883. continue;
  884. }
  885. LLRenderPass::applyModelMatrix(params);
  886. LLMaterial* matp = NULL;
  887. LLFetchedGLTFMaterial* gltfp = NULL;
  888. bool double_sided = false;
  889. bool gltf_alpha_blend = false;
  890. if (gUsePBRShaders)
  891. {
  892. gltfp = params.mGLTFMaterial.get();
  893. if (gltfp)
  894. {
  895. double_sided = gltfp->mDoubleSided;
  896. gltf_alpha_blend =
  897. gltfp->mAlphaMode == LLGLTFMaterial::ALPHA_MODE_BLEND;
  898. }
  899. if (!gltf_alpha_blend && !LLPipeline::sRenderingHUDs)
  900. {
  901. matp = params.mMaterial.get();
  902. }
  903. }
  904. else if (sDeferredRender)
  905. {
  906. matp = params.mMaterial.get();
  907. }
  908. LLGLDisable cull_face(double_sided ? GL_CULL_FACE : 0);
  909. if (gltf_alpha_blend)
  910. {
  911. if (params.mAvatar.notNull())
  912. {
  913. mTargetShader = mPBRShader->mRiggedVariant;
  914. }
  915. else
  916. {
  917. mTargetShader = mPBRShader;
  918. }
  919. if (LLGLSLShader::sCurBoundShaderPtr != mTargetShader)
  920. {
  921. gPipeline.bindDeferredShaderFast(*mTargetShader);
  922. }
  923. gltfp->bind(params.mTexture, params.mVSize);
  924. }
  925. else
  926. {
  927. bool bind_deferred = gUsePBRShaders;
  928. if (params.mFullbright)
  929. {
  930. if (light_enabled || !initialized_lighting)
  931. {
  932. initialized_lighting = true;
  933. mTargetShader = mFullbrightShader;
  934. light_enabled = false;
  935. }
  936. }
  937. // Turn on lighting if it is not already.
  938. else if (!light_enabled || !initialized_lighting)
  939. {
  940. initialized_lighting = true;
  941. mTargetShader = mSimpleShader;
  942. light_enabled = true;
  943. }
  944. if (gUsePBRShaders && LLPipeline::sRenderingHUDs)
  945. {
  946. mTargetShader = mFullbrightShader;
  947. }
  948. else if (matp)
  949. {
  950. U32 mask = params.mShaderMask;
  951. llassert(mask < LLMaterial::SHADER_COUNT);
  952. if (underwater)
  953. {
  954. mTargetShader = &gDeferredMaterialWaterProgram[mask];
  955. }
  956. else
  957. {
  958. mTargetShader = &gDeferredMaterialProgram[mask];
  959. }
  960. bind_deferred = true;
  961. }
  962. else if (params.mFullbright)
  963. {
  964. mTargetShader = mFullbrightShader;
  965. }
  966. else
  967. {
  968. mTargetShader = mSimpleShader;
  969. }
  970. if (params.mAvatar.notNull() && mTargetShader->mRiggedVariant)
  971. {
  972. mTargetShader = mTargetShader->mRiggedVariant;
  973. }
  974. // If we are not ALREADY using the proper shader, then bind it
  975. // (this way we do not rebind shaders unnecessarily).
  976. bool needs_binding =
  977. LLGLSLShader::sCurBoundShaderPtr != mTargetShader;
  978. if (!bind_deferred) // EE mode only, for non-materials. HB
  979. {
  980. if (needs_binding)
  981. {
  982. mTargetShader->bind();
  983. }
  984. }
  985. else if (needs_binding)
  986. {
  987. gPipeline.bindDeferredShaderFast(*mTargetShader);
  988. if (gUsePBRShaders && params.mFullbright)
  989. {
  990. // Make sure the bind the exposure map for fullbright
  991. // shaders so they can cancel out exposure.
  992. S32 chan =
  993. mTargetShader->enableTexture(LLShaderMgr::EXPOSURE_MAP);
  994. if (chan > -1)
  995. {
  996. gGL.getTexUnit(chan)->bind(&gPipeline.mExposureMap);
  997. }
  998. }
  999. }
  1000. LLVector4 spec_color(1.f, 1.f, 1.f, 1.f);
  1001. F32 env_intensity = 0.f;
  1002. F32 brightness = 1.f;
  1003. // If we have a material, supply the appropriate data here.
  1004. if (matp)
  1005. {
  1006. spec_color = params.mSpecColor;
  1007. env_intensity = params.mEnvIntensity;
  1008. brightness = params.mFullbright ? 1.f : 0.f;
  1009. }
  1010. mTargetShader->uniform4f(LLShaderMgr::SPECULAR_COLOR,
  1011. spec_color.mV[0], spec_color.mV[1],
  1012. spec_color.mV[2], spec_color.mV[3]);
  1013. mTargetShader->uniform1f(LLShaderMgr::ENVIRONMENT_INTENSITY,
  1014. env_intensity);
  1015. mTargetShader->uniform1f(LLShaderMgr::EMISSIVE_BRIGHTNESS,
  1016. brightness);
  1017. }
  1018. if (params.mAvatar.notNull() && params.mSkinInfo.notNull() &&
  1019. (params.mAvatar.get() != last_avatarp ||
  1020. params.mSkinInfo->mHash != last_hash ||
  1021. last_shaderp != mTargetShader))
  1022. {
  1023. if (!uploadMatrixPalette(params))
  1024. {
  1025. continue;
  1026. }
  1027. last_avatarp = params.mAvatar.get();
  1028. last_hash = params.mSkinInfo->mHash;
  1029. last_shaderp = mTargetShader;
  1030. }
  1031. bool tex_setup = texSetup(&params, matp != NULL, unit0);
  1032. {
  1033. LL_TRACY_TIMER(TRC_RENDER_ALPHA_DRAW);
  1034. gGL.blendFunc(params.mBlendFuncSrc, params.mBlendFuncDst,
  1035. mAlphaSFactor, mAlphaDFactor);
  1036. bool reset_minimum_alpha = false;
  1037. if (!LLPipeline::sImpostorRender &&
  1038. params.mBlendFuncDst != LLRender::BF_SOURCE_ALPHA &&
  1039. params.mBlendFuncSrc != LLRender::BF_SOURCE_ALPHA)
  1040. {
  1041. // This draw call has a custom blend function that may
  1042. // require rendering of "invisible" fragments
  1043. mTargetShader->setMinimumAlpha(0.f);
  1044. reset_minimum_alpha = true;
  1045. }
  1046. if (gUsePBRShaders)
  1047. {
  1048. params.mVertexBuffer->setBuffer();
  1049. params.mVertexBuffer->drawRange(LLRender::TRIANGLES,
  1050. params.mStart, params.mEnd,
  1051. params.mCount,
  1052. params.mOffset);
  1053. }
  1054. else
  1055. {
  1056. U32 draw_mask = mask;
  1057. if (params.mFullbright)
  1058. {
  1059. constexpr U32 FB_MASK =
  1060. ~(LLVertexBuffer::MAP_TANGENT |
  1061. LLVertexBuffer::MAP_TEXCOORD1 |
  1062. LLVertexBuffer::MAP_TEXCOORD2);
  1063. draw_mask &= FB_MASK;
  1064. }
  1065. if (params.mAvatar.notNull())
  1066. {
  1067. draw_mask |= LLVertexBuffer::MAP_WEIGHT4;
  1068. }
  1069. params.mVertexBuffer->setBufferFast(draw_mask);
  1070. params.mVertexBuffer->drawRangeFast(params.mStart,
  1071. params.mEnd,
  1072. params.mCount,
  1073. params.mOffset);
  1074. }
  1075. if (reset_minimum_alpha)
  1076. {
  1077. mTargetShader->setMinimumAlpha(MINIMUM_ALPHA);
  1078. }
  1079. }
  1080. // If this alpha mesh has glow, then draw it a second time to add
  1081. // the destination-alpha (=glow). Interleaving these state-changing
  1082. // calls is expensive, but glow must be drawn Z-sorted with alpha.
  1083. if (draw_glow && (!is_particle || params.mHasGlow) &&
  1084. !is_pre_water &&
  1085. params.mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_EMISSIVE))
  1086. {
  1087. LL_TRACY_TIMER(TRC_RENDER_ALPHA_EMISSIVE);
  1088. if (params.mAvatar.notNull())
  1089. {
  1090. if (gltfp)
  1091. {
  1092. pbr_rigged_emissives.push_back(&params);
  1093. }
  1094. else
  1095. {
  1096. rigged_emissives.push_back(&params);
  1097. }
  1098. }
  1099. else if (gltfp)
  1100. {
  1101. pbr_emissives.push_back(&params);
  1102. }
  1103. else
  1104. {
  1105. emissives.push_back(&params);
  1106. }
  1107. }
  1108. // Restore tex setup
  1109. if (tex_setup)
  1110. {
  1111. unit0->activate();
  1112. // Note: activate() did change matrix mode to MM_TEXTURE, so
  1113. // the loadIdentity() call does apply to MM_TEXTURE. HB
  1114. gGL.loadIdentity();
  1115. gGL.matrixMode(LLRender::MM_MODELVIEW);
  1116. }
  1117. }
  1118. if (!depth_only)
  1119. {
  1120. gPipeline.enableLightsDynamic();
  1121. // Install glow-accumulating blend mode and do not touch color, but
  1122. // add to alpha (glow).
  1123. gGL.blendFunc(LLRender::BF_ZERO, LLRender::BF_ONE,
  1124. LLRender::BF_ONE, LLRender::BF_ONE);
  1125. bool rebind = false;
  1126. LLGLSLShader* last_shaderp = LLGLSLShader::sCurBoundShaderPtr;
  1127. if (!emissives.empty())
  1128. {
  1129. light_enabled = true;
  1130. renderEmissives(mask, emissives);
  1131. rebind = true;
  1132. }
  1133. if (!pbr_emissives.empty())
  1134. {
  1135. light_enabled = true;
  1136. renderPbrEmissives(pbr_emissives);
  1137. rebind = true;
  1138. }
  1139. if (!rigged_emissives.empty())
  1140. {
  1141. light_enabled = false;
  1142. renderRiggedEmissives(mask, rigged_emissives);
  1143. rebind = true;
  1144. }
  1145. if (!pbr_rigged_emissives.empty())
  1146. {
  1147. light_enabled = true;
  1148. renderRiggedPbrEmissives(pbr_rigged_emissives);
  1149. rebind = true;
  1150. }
  1151. // Restore our alpha blend mode
  1152. gGL.blendFunc(mColorSFactor, mColorDFactor, mAlphaSFactor,
  1153. mAlphaDFactor);
  1154. if (rebind && last_shaderp)
  1155. {
  1156. last_shaderp->bind();
  1157. }
  1158. }
  1159. }
  1160. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  1161. LLVertexBuffer::unbind();
  1162. if (!light_enabled)
  1163. {
  1164. gPipeline.enableLightsDynamic();
  1165. }
  1166. }