lldrawpoolbump.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. /**
  2. * @file lldrawpoolbump.cpp
  3. * @brief LLDrawPoolBump class implementation
  4. *
  5. * $LicenseInfo:firstyear=2003&license=viewergpl$
  6. *
  7. * Copyright (c) 2003-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 <utility>
  34. #include "lldrawpoolbump.h"
  35. #include "llcubemap.h"
  36. #include "lldir.h"
  37. #include "llfasttimer.h"
  38. #include "llglheaders.h"
  39. #include "llimagegl.h"
  40. #include "llmodel.h"
  41. #include "llrender.h"
  42. #include "lltextureentry.h"
  43. #include "llappviewer.h" // For gFrameTimeSeconds
  44. #include "lldrawable.h"
  45. #include "llface.h"
  46. #include "llpipeline.h"
  47. #include "llsky.h"
  48. #include "llspatialpartition.h"
  49. #include "llviewercamera.h"
  50. #include "llviewercontrol.h"
  51. #include "llviewershadermgr.h"
  52. #include "llviewertexturelist.h"
  53. //static
  54. LLStandardBumpmap gStandardBumpmapList[TEM_BUMPMAP_COUNT];
  55. //static
  56. U32 LLStandardBumpmap::sStandardBumpmapCount = 0;
  57. //static
  58. LLBumpImageList gBumpImageList;
  59. constexpr S32 STD_BUMP_LATEST_FILE_VERSION = 1;
  60. constexpr U32 VERTEX_MASK_SHINY = LLVertexBuffer::MAP_VERTEX |
  61. LLVertexBuffer::MAP_NORMAL |
  62. LLVertexBuffer::MAP_COLOR;
  63. constexpr U32 VERTEX_MASK_BUMP = LLVertexBuffer::MAP_VERTEX |
  64. LLVertexBuffer::MAP_TEXCOORD0 |
  65. LLVertexBuffer::MAP_TEXCOORD1;
  66. U32 LLDrawPoolBump::sVertexMask = VERTEX_MASK_SHINY;
  67. static LLGLSLShader* sCurrentShader = NULL;
  68. static S32 sCubeChannel = -1;
  69. static S32 sDiffuseChannel = -1;
  70. static S32 sBumpChannel = -1;
  71. LLRenderTarget LLBumpImageList::sRenderTarget;
  72. //static
  73. void LLStandardBumpmap::add()
  74. {
  75. if (!gTextureList.isInitialized())
  76. {
  77. // Note: loading pre-configuration sometimes triggers this call.
  78. // But it is safe to return here because bump images will be reloaded
  79. // during initialization later.
  80. return;
  81. }
  82. // Cannot assert; we destroyGL and restoreGL a lot during *first* startup,
  83. // which populates this list already, THEN we explicitly init the list as
  84. // part of *normal* startup. Sigh. So clear the list every time before we
  85. // (re-)add the standard bumpmaps.
  86. //llassert(LLStandardBumpmap::sStandardBumpmapCount == 0);
  87. clear();
  88. llinfos << "Adding standard bumpmaps." << llendl;
  89. gStandardBumpmapList[sStandardBumpmapCount++] =
  90. LLStandardBumpmap("None"); // BE_NO_BUMP
  91. gStandardBumpmapList[sStandardBumpmapCount++] =
  92. LLStandardBumpmap("Brightness"); // BE_BRIGHTNESS
  93. gStandardBumpmapList[sStandardBumpmapCount++] =
  94. LLStandardBumpmap("Darkness"); // BE_DARKNESS
  95. std::string file_name = gDirUtil.getFullPath(LL_PATH_APP_SETTINGS,
  96. "std_bump.ini");
  97. LLFILE* file = LLFile::open(file_name, "rt");
  98. if (!file)
  99. {
  100. llwarns << "Could not open std_bump <" << file_name << ">" << llendl;
  101. return;
  102. }
  103. S32 file_version = 0;
  104. S32 fields_read = fscanf(file, "LLStandardBumpmap version %d",
  105. &file_version);
  106. if (fields_read != 1)
  107. {
  108. llwarns << "Bad LLStandardBumpmap header" << llendl;
  109. LLFile::close(file);
  110. return;
  111. }
  112. if (file_version > STD_BUMP_LATEST_FILE_VERSION)
  113. {
  114. llwarns << "LLStandardBumpmap has newer version (" << file_version
  115. << ") than viewer (" << STD_BUMP_LATEST_FILE_VERSION << ")"
  116. << llendl;
  117. LLFile::close(file);
  118. return;
  119. }
  120. while (!feof(file) &&
  121. LLStandardBumpmap::sStandardBumpmapCount < (U32)TEM_BUMPMAP_COUNT)
  122. {
  123. // *NOTE: This buffer size is hard coded into scanf() below.
  124. char label[256] = "";
  125. char bump_image_id[256] = "";
  126. fields_read = fscanf(file, "\n%255s %255s", label, bump_image_id);
  127. bump_image_id[UUID_STR_LENGTH - 1] = 0; // Truncate file name to UUID
  128. if (fields_read == EOF)
  129. {
  130. break;
  131. }
  132. if (fields_read != 2)
  133. {
  134. llwarns << "Bad LLStandardBumpmap entry" << llendl;
  135. break;
  136. }
  137. LLStandardBumpmap& bump =
  138. gStandardBumpmapList[sStandardBumpmapCount++];
  139. bump.mLabel = label;
  140. bump.mImage =
  141. LLViewerTextureManager::getFetchedTexture(LLUUID(bump_image_id));
  142. bump.mImage->setBoostLevel(LLGLTexture::BOOST_BUMP);
  143. #if !LL_IMPLICIT_SETNODELETE
  144. bump.mImage->setNoDelete();
  145. #endif
  146. bump.mImage->setLoadedCallback(LLBumpImageList::onSourceStandardLoaded,
  147. 0, true, false, NULL, NULL);
  148. bump.mImage->forceToSaveRawImage(0, 30.f);
  149. }
  150. LLFile::close(file);
  151. }
  152. //static
  153. void LLStandardBumpmap::clear()
  154. {
  155. if (sStandardBumpmapCount)
  156. {
  157. llinfos << "Clearing standard bumpmaps." << llendl;
  158. for (U32 i = 0; i < LLStandardBumpmap::sStandardBumpmapCount; ++i)
  159. {
  160. gStandardBumpmapList[i].mLabel.clear();
  161. gStandardBumpmapList[i].mImage = NULL;
  162. }
  163. sStandardBumpmapCount = 0;
  164. }
  165. }
  166. ////////////////////////////////////////////////////////////////
  167. //virtual
  168. void LLDrawPoolBump::prerender()
  169. {
  170. mShaderLevel =
  171. gViewerShaderMgrp->getShaderLevel(LLViewerShaderMgr::SHADER_OBJECT);
  172. }
  173. // For the EE renderer only
  174. //virtual
  175. void LLDrawPoolBump::render(S32 pass)
  176. {
  177. LL_FAST_TIMER(FTM_RENDER_BUMP);
  178. if (!gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_BUMP))
  179. {
  180. return;
  181. }
  182. for (U32 rigged = 0; rigged < 2; ++rigged)
  183. {
  184. mRigged = rigged;
  185. // First pass: shiny
  186. beginShiny();
  187. renderShiny();
  188. endShiny();
  189. // Second pass: fullbright shiny
  190. if (mShaderLevel > 1)
  191. {
  192. beginFullbrightShiny();
  193. renderFullbrightShiny();
  194. endFullbrightShiny();
  195. }
  196. // Third pass: bump
  197. beginBump();
  198. renderBump(PASS_BUMP);
  199. endBump();
  200. }
  201. }
  202. // For the EE renderer only
  203. //static
  204. void LLDrawPoolBump::bindCubeMap(LLGLSLShader* shaderp, S32 shader_level,
  205. S32& diffuse_channel, S32& cube_channel)
  206. {
  207. LLCubeMap* cubemapp = gSky.mVOSkyp ? gSky.mVOSkyp->getCubeMap() : NULL;
  208. if (!cubemapp)
  209. {
  210. return;
  211. }
  212. LLTexUnit* unit0 = gGL.getTexUnit(0);
  213. if (shaderp)
  214. {
  215. LLMatrix4 mat(gGLModelView.getF32ptr());
  216. LLVector3 vec = LLVector3(gShinyOrigin) * mat;
  217. LLVector4 vec4(vec, gShinyOrigin.mV[3]);
  218. shaderp->uniform4fv(LLShaderMgr::SHINY_ORIGIN, 1, vec4.mV);
  219. if (shader_level > 1)
  220. {
  221. cubemapp->setMatrix(1);
  222. // Make sure that texture coord generation happens for tex unit 1,
  223. // as this is the one we use for the cube map in the one pass shiny
  224. // shaders
  225. cube_channel = shaderp->enableTexture(LLShaderMgr::ENVIRONMENT_MAP,
  226. LLTexUnit::TT_CUBE_MAP);
  227. cubemapp->enableTexture(cube_channel);
  228. diffuse_channel = shaderp->enableTexture(LLShaderMgr::DIFFUSE_MAP);
  229. }
  230. else
  231. {
  232. cubemapp->setMatrix(0);
  233. cube_channel = shaderp->enableTexture(LLShaderMgr::ENVIRONMENT_MAP,
  234. LLTexUnit::TT_CUBE_MAP);
  235. diffuse_channel = -1;
  236. cubemapp->enableTexture(cube_channel);
  237. }
  238. gGL.getTexUnit(cube_channel)->bind(cubemapp);
  239. unit0->activate();
  240. }
  241. else
  242. {
  243. cube_channel = 0;
  244. diffuse_channel = -1;
  245. unit0->disable();
  246. cubemapp->enableTexture(0);
  247. cubemapp->setMatrix(0);
  248. unit0->bind(cubemapp);
  249. }
  250. }
  251. // For the EE renderer only
  252. //static
  253. void LLDrawPoolBump::unbindCubeMap(LLGLSLShader* shaderp, S32 shader_level,
  254. S32& diffuse_channel)
  255. {
  256. LLCubeMap* cubemapp = gSky.mVOSkyp ? gSky.mVOSkyp->getCubeMap() : NULL;
  257. if (!cubemapp)
  258. {
  259. return;
  260. }
  261. if (shader_level > 1)
  262. {
  263. shaderp->disableTexture(LLShaderMgr::ENVIRONMENT_MAP,
  264. LLTexUnit::TT_CUBE_MAP);
  265. #if 0 // 'shader_level' is in fact mShaderLevel, which is itself set to
  266. // getShaderLevel(LLViewerShaderMgr::SHADER_OBJECT) in prerender(), so
  267. // there no need to test this (which always true)... HB
  268. if (gViewerShaderMgrp->getShaderLevel(LLViewerShaderMgr::SHADER_OBJECT) > 0)
  269. #endif
  270. {
  271. if (diffuse_channel)
  272. {
  273. shaderp->disableTexture(LLShaderMgr::DIFFUSE_MAP);
  274. }
  275. }
  276. }
  277. // Moved below shaderp->disableTexture call to avoid false alarms from
  278. // auto-re-enable of textures on stage 0 - MAINT-755
  279. cubemapp->disableTexture();
  280. cubemapp->restoreMatrix();
  281. }
  282. // For the EE renderer only
  283. void LLDrawPoolBump::beginShiny()
  284. {
  285. LL_FAST_TIMER(FTM_RENDER_SHINY);
  286. mShiny = true;
  287. sVertexMask = VERTEX_MASK_SHINY;
  288. // Second pass: environment map
  289. if (mShaderLevel > 1)
  290. {
  291. sVertexMask = VERTEX_MASK_SHINY | LLVertexBuffer::MAP_TEXCOORD0;
  292. }
  293. if (LLPipeline::sUnderWaterRender)
  294. {
  295. sCurrentShader = &gObjectShinyWaterProgram;
  296. }
  297. else
  298. {
  299. sCurrentShader = &gObjectShinyProgram;
  300. }
  301. if (mRigged && sCurrentShader->mRiggedVariant)
  302. {
  303. sCurrentShader = sCurrentShader->mRiggedVariant;
  304. }
  305. sCurrentShader->bind();
  306. S32 no_atmo = LLPipeline::sRenderingHUDs ? 1 : 0;
  307. sCurrentShader->uniform1i(LLShaderMgr::NO_ATMO, no_atmo);
  308. bindCubeMap(sCurrentShader, mShaderLevel, sDiffuseChannel, sCubeChannel);
  309. if (mShaderLevel > 1)
  310. {
  311. // Indexed texture rendering, channel 0 is always diffuse
  312. sDiffuseChannel = 0;
  313. }
  314. }
  315. // For the EE renderer only
  316. void LLDrawPoolBump::renderShiny()
  317. {
  318. LL_FAST_TIMER(FTM_RENDER_SHINY);
  319. if (!gSky.mVOSkyp->getCubeMap())
  320. {
  321. return;
  322. }
  323. LLGLEnable blend_enable(GL_BLEND);
  324. if (mShaderLevel > 1)
  325. {
  326. U32 mask = sVertexMask | LLVertexBuffer::MAP_TEXTURE_INDEX;
  327. if (mRigged)
  328. {
  329. LLRenderPass::pushRiggedBatches(PASS_SHINY_RIGGED, mask, true,
  330. true);
  331. }
  332. else
  333. {
  334. LLRenderPass::pushBatches(PASS_SHINY, mask, true, true);
  335. }
  336. }
  337. else if (mRigged)
  338. {
  339. gPipeline.renderRiggedGroups(this, PASS_SHINY_RIGGED, sVertexMask,
  340. true);
  341. }
  342. else
  343. {
  344. gPipeline.renderGroups(this, PASS_SHINY, sVertexMask, true);
  345. }
  346. }
  347. // For the EE renderer only
  348. void LLDrawPoolBump::endShiny()
  349. {
  350. LL_FAST_TIMER(FTM_RENDER_SHINY);
  351. unbindCubeMap(sCurrentShader, mShaderLevel, sDiffuseChannel);
  352. if (sCurrentShader)
  353. {
  354. sCurrentShader->unbind();
  355. }
  356. sDiffuseChannel = -1;
  357. sCubeChannel = 0;
  358. mShiny = false;
  359. }
  360. void LLDrawPoolBump::beginFullbrightShiny()
  361. {
  362. LL_FAST_TIMER(FTM_RENDER_SHINY);
  363. sVertexMask = VERTEX_MASK_SHINY | LLVertexBuffer::MAP_TEXCOORD0;
  364. // Second pass: environment map
  365. if (gUsePBRShaders)
  366. {
  367. sCurrentShader =
  368. LLPipeline::sRenderingHUDs ? &gHUDFullbrightShinyProgram
  369. : &gDeferredFullbrightShinyProgram;
  370. }
  371. else if (LLPipeline::sUnderWaterRender)
  372. {
  373. sCurrentShader = &gObjectFullbrightShinyWaterProgram;
  374. }
  375. else if (LLPipeline::sRenderDeferred)
  376. {
  377. sCurrentShader = &gDeferredFullbrightShinyProgram;
  378. }
  379. else
  380. {
  381. sCurrentShader = &gObjectFullbrightShinyProgram;
  382. }
  383. if (mRigged)
  384. {
  385. if (sCurrentShader->mRiggedVariant)
  386. {
  387. sCurrentShader = sCurrentShader->mRiggedVariant;
  388. }
  389. else
  390. {
  391. llwarns_once << "Missing rigged variant shader !" << llendl;
  392. }
  393. }
  394. if (gUsePBRShaders)
  395. {
  396. // Bind exposure map so fullbright shader can cancel out exposure
  397. S32 channel = sCurrentShader->enableTexture(LLShaderMgr::EXPOSURE_MAP);
  398. if (channel > -1)
  399. {
  400. gGL.getTexUnit(channel)->bind(&gPipeline.mExposureMap);
  401. }
  402. }
  403. LLCubeMap* cubemapp = gSky.mVOSkyp ? gSky.mVOSkyp->getCubeMap() : NULL;
  404. if (gUsePBRShaders)
  405. {
  406. if (cubemapp && !LLPipeline::sReflectionProbesEnabled)
  407. {
  408. // Make sure that texture coord generation happens for tex unit 1,
  409. // as this is the one we use for the cube map in the one pass shiny
  410. // shaders
  411. gGL.getTexUnit(1)->disable();
  412. sCubeChannel =
  413. sCurrentShader->enableTexture(LLShaderMgr::ENVIRONMENT_MAP,
  414. LLTexUnit::TT_CUBE_MAP);
  415. // Note: a no-operation when sCubeChannel == -1. HB
  416. cubemapp->enableTexture(sCubeChannel);
  417. sDiffuseChannel =
  418. sCurrentShader->enableTexture(LLShaderMgr::DIFFUSE_MAP);
  419. if (sCubeChannel > -1) // Must be tested here. HB
  420. {
  421. gGL.getTexUnit(sCubeChannel)->bind(cubemapp);
  422. }
  423. gGL.getTexUnit(0)->activate();
  424. }
  425. LLMatrix4 mat(gGLModelView.getF32ptr());
  426. sCurrentShader->bind();
  427. LLVector3 vec = LLVector3(gShinyOrigin) * mat;
  428. LLVector4 vec4(vec, gShinyOrigin.mV[3]);
  429. sCurrentShader->uniform4fv(LLShaderMgr::SHINY_ORIGIN, 1, vec4.mV);
  430. if (LLPipeline::sReflectionProbesEnabled)
  431. {
  432. gPipeline.bindReflectionProbes(*sCurrentShader);
  433. }
  434. else
  435. {
  436. gPipeline.setEnvMat(*sCurrentShader);
  437. }
  438. }
  439. else if (cubemapp)
  440. {
  441. LLMatrix4 mat(gGLModelView.getF32ptr());
  442. sCurrentShader->bind();
  443. S32 no_atmo = LLPipeline::sRenderingHUDs ? 1 : 0;
  444. sCurrentShader->uniform1i(LLShaderMgr::NO_ATMO, no_atmo);
  445. LLVector3 vec = LLVector3(gShinyOrigin) * mat;
  446. LLVector4 vec4(vec, gShinyOrigin.mV[3]);
  447. sCurrentShader->uniform4fv(LLShaderMgr::SHINY_ORIGIN, 1, vec4.mV);
  448. cubemapp->setMatrix(1);
  449. // Make sure that texture coord generation happens for tex unit 1, as
  450. // this is the one we use for the cube map in the one pass shiny
  451. // shaders.
  452. gGL.getTexUnit(1)->disable();
  453. sCubeChannel =
  454. sCurrentShader->enableTexture(LLShaderMgr::ENVIRONMENT_MAP,
  455. LLTexUnit::TT_CUBE_MAP);
  456. cubemapp->enableTexture(sCubeChannel);
  457. sDiffuseChannel =
  458. sCurrentShader->enableTexture(LLShaderMgr::DIFFUSE_MAP);
  459. gGL.getTexUnit(sCubeChannel)->bind(cubemapp);
  460. gGL.getTexUnit(0)->activate();
  461. }
  462. if (mShaderLevel > 1)
  463. {
  464. // Indexed texture rendering, channel 0 is always diffuse
  465. sDiffuseChannel = 0;
  466. }
  467. mShiny = true;
  468. }
  469. void LLDrawPoolBump::renderFullbrightShiny()
  470. {
  471. LL_FAST_TIMER(FTM_RENDER_SHINY);
  472. if (!gUsePBRShaders && !gSky.mVOSkyp->getCubeMap())
  473. {
  474. return;
  475. }
  476. LLGLEnable blend_enable(GL_BLEND);
  477. if (mShaderLevel > 1)
  478. {
  479. // Note: 'mask' is ignored for the PBR renderer
  480. U32 mask = sVertexMask | LLVertexBuffer::MAP_TEXTURE_INDEX;
  481. if (mRigged)
  482. {
  483. LLRenderPass::pushRiggedBatches(PASS_FULLBRIGHT_SHINY_RIGGED, mask,
  484. true, true);
  485. }
  486. else
  487. {
  488. LLRenderPass::pushBatches(PASS_FULLBRIGHT_SHINY, mask, true, true);
  489. }
  490. }
  491. else if (mRigged)
  492. {
  493. LLRenderPass::pushRiggedBatches(PASS_FULLBRIGHT_SHINY_RIGGED,
  494. sVertexMask);
  495. }
  496. else
  497. {
  498. LLRenderPass::pushBatches(PASS_FULLBRIGHT_SHINY, sVertexMask);
  499. }
  500. }
  501. void LLDrawPoolBump::endFullbrightShiny()
  502. {
  503. LL_FAST_TIMER(FTM_RENDER_SHINY);
  504. LLCubeMap* cubemapp = gSky.mVOSkyp ? gSky.mVOSkyp->getCubeMap() : NULL;
  505. if (cubemapp && !LLPipeline::sReflectionProbesEnabled)
  506. {
  507. cubemapp->disableTexture();
  508. if (!gUsePBRShaders)
  509. {
  510. cubemapp->restoreMatrix();
  511. }
  512. else if (sCurrentShader->mFeatures.hasReflectionProbes)
  513. {
  514. gPipeline.unbindReflectionProbes(*sCurrentShader);
  515. }
  516. sCurrentShader->unbind();
  517. }
  518. sDiffuseChannel = -1;
  519. sCubeChannel = 0;
  520. mShiny = false;
  521. }
  522. void LLDrawPoolBump::renderGroup(LLSpatialGroup* groupp, U32 type, U32 mask,
  523. bool texture)
  524. {
  525. LLSpatialGroup::drawmap_elem_t& draw_info = groupp->mDrawMap[type];
  526. for (U32 i = 0, count = draw_info.size(); i < count; ++i)
  527. {
  528. LLDrawInfo& params = *draw_info[i];
  529. applyModelMatrix(params);
  530. // Note: mask is ignored by the PBR renderer.
  531. params.mVertexBuffer->setBuffer(mask);
  532. params.mVertexBuffer->drawRange(LLRender::TRIANGLES, params.mStart,
  533. params.mEnd, params.mCount,
  534. params.mOffset);
  535. gPipeline.addTrianglesDrawn(params.mCount);
  536. }
  537. }
  538. //static
  539. bool LLDrawPoolBump::bindBumpMap(LLDrawInfo& params, S32 channel)
  540. {
  541. return bindBumpMap(params.mBump, params.mTexture, params.mVSize, channel);
  542. }
  543. //static
  544. bool LLDrawPoolBump::bindBumpMap(LLFace* facep, S32 channel)
  545. {
  546. const LLTextureEntry* tep = facep->getTextureEntry();
  547. return tep && bindBumpMap(tep->getBumpmap(), facep->getTexture(),
  548. facep->getVirtualSize(), channel);
  549. }
  550. //static
  551. bool LLDrawPoolBump::bindBumpMap(U8 bump_code, LLViewerTexture* texturep,
  552. F32 vsize, S32 channel)
  553. {
  554. LLViewerFetchedTexture* texp =
  555. LLViewerTextureManager::staticCast(texturep);
  556. if (!texp)
  557. {
  558. // If the texture is not a fetched texture
  559. return false;
  560. }
  561. LLViewerTexture* bumpp = NULL;
  562. switch (bump_code)
  563. {
  564. case BE_NO_BUMP:
  565. break;
  566. case BE_BRIGHTNESS:
  567. case BE_DARKNESS:
  568. bumpp = gBumpImageList.getBrightnessDarknessImage(texp, bump_code);
  569. break;
  570. default:
  571. if (bump_code < LLStandardBumpmap::sStandardBumpmapCount)
  572. {
  573. bumpp = gStandardBumpmapList[bump_code].mImage;
  574. gBumpImageList.addTextureStats(bump_code, texp->getID(),
  575. vsize);
  576. }
  577. }
  578. if (!bumpp)
  579. {
  580. return false;
  581. }
  582. if (channel == -2)
  583. {
  584. gGL.getTexUnit(1)->bindFast(bumpp);
  585. gGL.getTexUnit(0)->bindFast(bumpp);
  586. }
  587. else
  588. {
  589. // NOTE: do not use bindFast here (see SL-16222)
  590. gGL.getTexUnit(channel)->bind(bumpp);
  591. }
  592. return true;
  593. }
  594. // Optional second pass: emboss bump map
  595. //static
  596. void LLDrawPoolBump::beginBump()
  597. {
  598. LL_FAST_TIMER(FTM_RENDER_BUMP);
  599. // Optional second pass: emboss bump map
  600. sVertexMask = VERTEX_MASK_BUMP;
  601. sCurrentShader = &gObjectBumpProgram;
  602. if (mRigged)
  603. {
  604. if (sCurrentShader->mRiggedVariant)
  605. {
  606. sCurrentShader = sCurrentShader->mRiggedVariant;
  607. }
  608. else
  609. {
  610. llwarns_once << "Missing rigged variant shader !" << llendl;
  611. }
  612. }
  613. sCurrentShader->bind();
  614. gGL.setSceneBlendType(LLRender::BT_MULT_X2);
  615. stop_glerror();
  616. }
  617. void LLDrawPoolBump::renderBump(U32 pass)
  618. {
  619. LL_FAST_TIMER(FTM_RENDER_BUMP);
  620. LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE, GL_LEQUAL);
  621. LLGLEnable blend(GL_BLEND);
  622. gGL.diffuseColor4f(1.f, 1.f, 1.f, 1.f);
  623. // Get rid of z-fighting with non-bump pass.
  624. LLGLEnable poly_offset(GL_POLYGON_OFFSET_FILL);
  625. glPolygonOffset(-1.f, -1.f);
  626. if (gUsePBRShaders)
  627. {
  628. pushBumpBatches(pass);
  629. }
  630. else
  631. {
  632. renderBump(pass, VERTEX_MASK_BUMP);
  633. }
  634. }
  635. //static
  636. void LLDrawPoolBump::endBump()
  637. {
  638. if (gUsePBRShaders)
  639. {
  640. LLGLSLShader::unbind();
  641. }
  642. else
  643. {
  644. sCurrentShader->unbind();
  645. }
  646. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  647. }
  648. void LLDrawPoolBump::renderDeferred(S32 pass)
  649. {
  650. LL_FAST_TIMER(FTM_RENDER_BUMP);
  651. if (!gPipeline.sCull)
  652. {
  653. // Paranoia (sCull != NULL needed for getRenderMap())
  654. return;
  655. }
  656. mShiny = true;
  657. LLTexUnit* unit0 = gGL.getTexUnit(0);
  658. constexpr U32 mask = LLVertexBuffer::MAP_VERTEX |
  659. LLVertexBuffer::MAP_TEXCOORD0 |
  660. LLVertexBuffer::MAP_TANGENT |
  661. LLVertexBuffer::MAP_NORMAL |
  662. LLVertexBuffer::MAP_COLOR;
  663. LLVOAvatar* last_avatarp = NULL;
  664. U64 last_hash = 0;
  665. for (U32 rigged = 0; rigged < 2; ++rigged)
  666. {
  667. gDeferredBumpProgram.bind(rigged);
  668. LLGLSLShader* shaderp = LLGLSLShader::sCurBoundShaderPtr;
  669. sDiffuseChannel = shaderp->enableTexture(LLShaderMgr::DIFFUSE_MAP);
  670. sBumpChannel = shaderp->enableTexture(LLShaderMgr::BUMP_MAP);
  671. gGL.getTexUnit(sDiffuseChannel)->unbind(LLTexUnit::TT_TEXTURE);
  672. gGL.getTexUnit(sBumpChannel)->unbind(LLTexUnit::TT_TEXTURE);
  673. U32 type = rigged ? PASS_BUMP_RIGGED : PASS_BUMP;
  674. LLCullResult::drawinfo_list_t& dlist = gPipeline.getRenderMap(type);
  675. for (U32 i = 0, count = dlist.size(); i < count; )
  676. {
  677. LLDrawInfo* paramsp = dlist[i++];
  678. // Draw info cache prefetching optimization.
  679. if (i < count)
  680. {
  681. _mm_prefetch((char*)dlist[i]->mVertexBuffer.get(),
  682. _MM_HINT_NTA);
  683. if (i + 1 < count)
  684. {
  685. _mm_prefetch((char*)dlist[i + 1], _MM_HINT_NTA);
  686. }
  687. }
  688. shaderp->setMinimumAlpha(paramsp->mAlphaMaskCutoff);
  689. bindBumpMap(*paramsp, sBumpChannel);
  690. if (rigged)
  691. {
  692. if (paramsp->mAvatar && paramsp->mSkinInfo &&
  693. (paramsp->mAvatar != last_avatarp ||
  694. paramsp->mSkinInfo->mHash != last_hash))
  695. {
  696. #if 0
  697. if (!uploadMatrixPalette(*paramsp))
  698. {
  699. continue;
  700. }
  701. #else
  702. uploadMatrixPalette(*paramsp);
  703. #endif
  704. last_avatarp = paramsp->mAvatar;
  705. last_hash = paramsp->mSkinInfo->mHash;
  706. }
  707. pushBumpBatch(*paramsp, mask | LLVertexBuffer::MAP_WEIGHT4,
  708. true, false);
  709. }
  710. else
  711. {
  712. pushBumpBatch(*paramsp, mask, true, false);
  713. }
  714. }
  715. shaderp->disableTexture(LLShaderMgr::DIFFUSE_MAP);
  716. shaderp->disableTexture(LLShaderMgr::BUMP_MAP);
  717. shaderp->unbind();
  718. unit0->activate();
  719. }
  720. mShiny = false;
  721. }
  722. void LLDrawPoolBump::renderPostDeferred(S32 pass)
  723. {
  724. // Skip rigged pass when rendering HUDs
  725. U32 num_passes = LLPipeline::sRenderingHUDs ? 1 : 2;
  726. // Two passes: static and rigged
  727. for (U32 rigged = 0; rigged < num_passes; ++rigged)
  728. {
  729. mRigged = rigged;
  730. // Render shiny
  731. beginFullbrightShiny();
  732. renderFullbrightShiny();
  733. endFullbrightShiny();
  734. // Render bump
  735. beginBump();
  736. renderBump(PASS_POST_BUMP);
  737. endBump();
  738. }
  739. }
  740. ////////////////////////////////////////////////////////////////
  741. // List of bump-maps created from other textures.
  742. void LLBumpImageList::destroyGL()
  743. {
  744. // These will be re-populated on-demand
  745. if (!mBrightnessEntries.empty() && !mDarknessEntries.empty())
  746. {
  747. llinfos << "Clearing dynamic bumpmaps." << llendl;
  748. mBrightnessEntries.clear();
  749. mDarknessEntries.clear();
  750. }
  751. LLStandardBumpmap::shutdown();
  752. }
  753. void LLBumpImageList::restoreGL()
  754. {
  755. if (!gTextureList.isInitialized())
  756. {
  757. // Safe to return here because bump images will be reloaded during
  758. // initialization later.
  759. return;
  760. }
  761. LLStandardBumpmap::init();
  762. // Images will be recreated as they are needed.
  763. }
  764. // Note: Does nothing for entries in gStandardBumpmapList that are not actually
  765. // standard bump images (e.g. none, brightness, and darkness)
  766. void LLBumpImageList::addTextureStats(U8 bump, const LLUUID& base_image_id,
  767. F32 virtual_size)
  768. {
  769. bump &= TEM_BUMP_MASK;
  770. LLViewerFetchedTexture* bump_image = gStandardBumpmapList[bump].mImage;
  771. if (bump_image)
  772. {
  773. bump_image->addTextureStats(virtual_size);
  774. }
  775. }
  776. void LLBumpImageList::updateImages()
  777. {
  778. for (bump_image_map_t::iterator iter = mBrightnessEntries.begin(),
  779. end = mBrightnessEntries.end();
  780. iter != end; )
  781. {
  782. bump_image_map_t::iterator curiter = iter++;
  783. LLViewerTexture* image = curiter->second;
  784. if (image)
  785. {
  786. bool destroy = true;
  787. if (image->hasGLTexture())
  788. {
  789. if (image->getBoundRecently())
  790. {
  791. destroy = false;
  792. }
  793. else
  794. {
  795. image->destroyGLTexture();
  796. }
  797. }
  798. if (destroy)
  799. {
  800. // Deletes the image thanks to reference counting
  801. mBrightnessEntries.erase(curiter);
  802. }
  803. }
  804. }
  805. for (bump_image_map_t::iterator iter = mDarknessEntries.begin(),
  806. end = mDarknessEntries.end();
  807. iter != end; )
  808. {
  809. bump_image_map_t::iterator curiter = iter++;
  810. LLViewerTexture* image = curiter->second;
  811. if (image)
  812. {
  813. bool destroy = true;
  814. if (image->hasGLTexture())
  815. {
  816. if (image->getBoundRecently())
  817. {
  818. destroy = false;
  819. }
  820. else
  821. {
  822. image->destroyGLTexture();
  823. }
  824. }
  825. if (destroy)
  826. {
  827. // Deletes the image thanks to reference counting
  828. mDarknessEntries.erase(curiter);
  829. }
  830. }
  831. }
  832. }
  833. // Note: the caller SHOULD NOT keep the pointer that this function returns.
  834. // It may be updated as more data arrives.
  835. LLViewerTexture* LLBumpImageList::getBrightnessDarknessImage(LLViewerFetchedTexture* src_image,
  836. U8 bump_code)
  837. {
  838. llassert(bump_code == BE_BRIGHTNESS || bump_code == BE_DARKNESS);
  839. LLViewerTexture* bump = NULL;
  840. bump_image_map_t* entries_list = NULL;
  841. void (*callback_func)(bool success, LLViewerFetchedTexture* src_vi,
  842. LLImageRaw* src, LLImageRaw* aux_src,
  843. S32 discard_level, bool is_final,
  844. void* userdata) = NULL;
  845. switch (bump_code)
  846. {
  847. case BE_BRIGHTNESS:
  848. entries_list = &mBrightnessEntries;
  849. callback_func = LLBumpImageList::onSourceBrightnessLoaded;
  850. break;
  851. case BE_DARKNESS:
  852. entries_list = &mDarknessEntries;
  853. callback_func = LLBumpImageList::onSourceDarknessLoaded;
  854. break;
  855. default:
  856. llassert(false);
  857. return NULL;
  858. }
  859. bump_image_map_t::iterator iter = entries_list->find(src_image->getID());
  860. if (iter != entries_list->end() && iter->second.notNull())
  861. {
  862. bump = iter->second;
  863. }
  864. else
  865. {
  866. (*entries_list)[src_image->getID()] =
  867. LLViewerTextureManager::getLocalTexture(true);
  868. // In case callback was called immediately and replaced the image:
  869. bump = (*entries_list)[src_image->getID()];
  870. }
  871. if (!src_image->hasCallbacks())
  872. {
  873. // If image has no callbacks but resolutions do not match, trigger
  874. // raw image loaded callback again
  875. if (src_image->getWidth() != bump->getWidth() ||
  876. #if 0
  877. (LLPipeline::sRenderDeferred && bump->getComponents() != 4) ||
  878. #endif
  879. src_image->getHeight() != bump->getHeight())
  880. {
  881. src_image->setBoostLevel(LLGLTexture::BOOST_BUMP);
  882. src_image->setLoadedCallback(callback_func, 0, true, false,
  883. new LLUUID(src_image->getID()), NULL);
  884. src_image->forceToSaveRawImage(0);
  885. }
  886. }
  887. return bump;
  888. }
  889. //static
  890. void LLBumpImageList::onSourceBrightnessLoaded(bool success,
  891. LLViewerFetchedTexture* src_vi,
  892. LLImageRaw* src,
  893. LLImageRaw* aux_src,
  894. S32 discard_level,
  895. bool is_final,
  896. void* userdata)
  897. {
  898. LLUUID* source_asset_id = (LLUUID*)userdata;
  899. LLBumpImageList::onSourceLoaded(success, src_vi, src, *source_asset_id,
  900. BE_BRIGHTNESS);
  901. if (is_final)
  902. {
  903. delete source_asset_id;
  904. }
  905. }
  906. //static
  907. void LLBumpImageList::onSourceDarknessLoaded(bool success,
  908. LLViewerFetchedTexture* src_vi,
  909. LLImageRaw* src,
  910. LLImageRaw* aux_src,
  911. S32 discard_level,
  912. bool is_final,
  913. void* userdata)
  914. {
  915. LLUUID* source_asset_id = (LLUUID*)userdata;
  916. LLBumpImageList::onSourceLoaded(success, src_vi, src, *source_asset_id,
  917. BE_DARKNESS);
  918. if (is_final)
  919. {
  920. delete source_asset_id;
  921. }
  922. }
  923. void LLBumpImageList::onSourceStandardLoaded(bool success,
  924. LLViewerFetchedTexture* src_vi,
  925. LLImageRaw* src,
  926. LLImageRaw* aux_src,
  927. S32 discard_level,
  928. bool, void*)
  929. {
  930. if (success && LLPipeline::sRenderDeferred)
  931. {
  932. LL_FAST_TIMER(FTM_BUMP_SOURCE_STANDARD_LOADED);
  933. LLPointer<LLImageRaw> nrm_image = new LLImageRaw(src->getWidth(),
  934. src->getHeight(), 4);
  935. {
  936. LL_FAST_TIMER(FTM_BUMP_GEN_NORMAL);
  937. generateNormalMapFromAlpha(src, nrm_image);
  938. }
  939. src_vi->setExplicitFormat(GL_RGBA, GL_RGBA);
  940. {
  941. LL_FAST_TIMER(FTM_BUMP_CREATE_TEXTURE);
  942. src_vi->createGLTexture(src_vi->getDiscardLevel(), nrm_image);
  943. }
  944. }
  945. }
  946. void LLBumpImageList::generateNormalMapFromAlpha(LLImageRaw* src,
  947. LLImageRaw* nrm_image)
  948. {
  949. U8* nrm_data = nrm_image->getData();
  950. S32 resx = src->getWidth();
  951. S32 resy = src->getHeight();
  952. U8* src_data = src->getData();
  953. S32 src_cmp = src->getComponents();
  954. static LLCachedControl<F32> norm_scale(gSavedSettings,
  955. "RenderNormalMapScale");
  956. // Generate normal map from pseudo-heightfield
  957. LLVector3 up, down, left, right, norm;
  958. up.mV[VY] = -norm_scale;
  959. down.mV[VY] = norm_scale;
  960. left.mV[VX] = -norm_scale;
  961. right.mV[VX] = norm_scale;
  962. static const LLVector3 offset(0.5f, 0.5f ,0.5f);
  963. U32 idx = 0;
  964. for (S32 j = 0; j < resy; ++j)
  965. {
  966. for (S32 i = 0; i < resx; ++i)
  967. {
  968. S32 rx = (i + 1) % resx;
  969. S32 ry = (j + 1) % resy;
  970. S32 lx = (i - 1) % resx;
  971. if (lx < 0)
  972. {
  973. lx += resx;
  974. }
  975. S32 ly = (j - 1) % resy;
  976. if (ly < 0)
  977. {
  978. ly += resy;
  979. }
  980. F32 ch = (F32)src_data[(j * resx + i) * src_cmp + src_cmp - 1];
  981. right.mV[VZ] = (F32)src_data[(j * resx + rx + 1) * src_cmp - 1] - ch;
  982. left.mV[VZ] = (F32)src_data[(j * resx + lx + 1) * src_cmp - 1] - ch;
  983. up.mV[VZ] = (F32)src_data[(ly * resx + i + 1) * src_cmp - 1] - ch;
  984. down.mV[VZ] = (F32)src_data[(ry * resx + i + 1) * src_cmp - 1] - ch;
  985. norm = right % down + down % left + left % up + up % right;
  986. norm.normalize();
  987. norm *= 0.5f;
  988. norm += offset;
  989. idx = (j * resx + i) * 4;
  990. nrm_data[idx] = (U8)(norm.mV[0] * 255);
  991. nrm_data[idx + 1] = (U8)(norm.mV[1] * 255);
  992. nrm_data[idx + 2] = (U8)(norm.mV[2] * 255);
  993. nrm_data[idx + 3] = src_data[(j * resx + i) * src_cmp + src_cmp - 1];
  994. }
  995. }
  996. }
  997. //static
  998. void LLBumpImageList::onSourceLoaded(bool success, LLViewerTexture* src_vi,
  999. LLImageRaw* src, LLUUID& source_asset_id,
  1000. EBumpEffect bump_code)
  1001. {
  1002. LL_FAST_TIMER(FTM_BUMP_SOURCE_LOADED);
  1003. if (!success)
  1004. {
  1005. return;
  1006. }
  1007. if (!src || !src->getData()) // Paranoia
  1008. {
  1009. llwarns << "No image data for bump texture: " << source_asset_id
  1010. << llendl;
  1011. return;
  1012. }
  1013. bump_image_map_t& entries_list(bump_code == BE_BRIGHTNESS ?
  1014. gBumpImageList.mBrightnessEntries :
  1015. gBumpImageList.mDarknessEntries);
  1016. bump_image_map_t::iterator iter = entries_list.find(source_asset_id);
  1017. bool needs_update = iter == entries_list.end() || iter->second.isNull() ||
  1018. iter->second->getWidth() != src->getWidth() ||
  1019. iter->second->getHeight() != src->getHeight();
  1020. if (needs_update)
  1021. {
  1022. // If bump not cached yet or has changed resolution...
  1023. LL_FAST_TIMER(FTM_BUMP_SOURCE_ENTRIES_UPDATE);
  1024. // Make sure an entry exists for this image
  1025. iter = entries_list.emplace(src_vi->getID(),
  1026. LLViewerTextureManager::getLocalTexture(true)).first;
  1027. }
  1028. else
  1029. {
  1030. // Nothing to do
  1031. return;
  1032. }
  1033. LLPointer<LLImageRaw> dst_image = new LLImageRaw(src->getWidth(),
  1034. src->getHeight(), 1);
  1035. if (dst_image.isNull())
  1036. {
  1037. llwarns << "Could not create a new raw image for bump: "
  1038. << src_vi->getID() << ". Out of memory !" << llendl;
  1039. return;
  1040. }
  1041. U8* dst_data = dst_image->getData();
  1042. S32 dst_data_size = dst_image->getDataSize();
  1043. U8* src_data = src->getData();
  1044. S32 src_data_size = src->getDataSize();
  1045. S32 src_components = src->getComponents();
  1046. // Convert to luminance and then scale and bias that to get ready for
  1047. // embossed bump mapping (0-255 maps to 127-255).
  1048. // Convert to fixed point so we don't have to worry about precision or
  1049. // clamping.
  1050. constexpr S32 FIXED_PT = 8;
  1051. constexpr S32 R_WEIGHT = S32(0.2995f * F32(1 << FIXED_PT));
  1052. constexpr S32 G_WEIGHT = S32(0.5875f * F32(1 << FIXED_PT));
  1053. constexpr S32 B_WEIGHT = S32(0.1145f * F32(1 << FIXED_PT));
  1054. S32 minimum = 255;
  1055. S32 maximum = 0;
  1056. switch (src_components)
  1057. {
  1058. case 1:
  1059. case 2:
  1060. {
  1061. LL_FAST_TIMER(FTM_BUMP_SOURCE_MIN_MAX);
  1062. if (src_data_size == dst_data_size * src_components)
  1063. {
  1064. for (S32 i = 0, j = 0; i < dst_data_size;
  1065. i++, j += src_components)
  1066. {
  1067. dst_data[i] = src_data[j];
  1068. if (dst_data[i] < minimum)
  1069. {
  1070. minimum = dst_data[i];
  1071. }
  1072. if (dst_data[i] > maximum)
  1073. {
  1074. maximum = dst_data[i];
  1075. }
  1076. }
  1077. }
  1078. else
  1079. {
  1080. llassert(false);
  1081. dst_image->clear();
  1082. }
  1083. break;
  1084. }
  1085. case 3:
  1086. case 4:
  1087. {
  1088. LL_FAST_TIMER(FTM_BUMP_SOURCE_RGB2LUM);
  1089. if (src_data_size == dst_data_size * src_components)
  1090. {
  1091. for (S32 i = 0, j = 0; i < dst_data_size;
  1092. i++, j+= src_components)
  1093. {
  1094. // RGB to luminance
  1095. dst_data[i] = (R_WEIGHT * src_data[j] +
  1096. G_WEIGHT * src_data[j + 1] +
  1097. B_WEIGHT * src_data[j + 2]) >> FIXED_PT;
  1098. if (dst_data[i] < minimum)
  1099. {
  1100. minimum = dst_data[i];
  1101. }
  1102. if (dst_data[i] > maximum)
  1103. {
  1104. maximum = dst_data[i];
  1105. }
  1106. }
  1107. }
  1108. else
  1109. {
  1110. llassert(false);
  1111. dst_image->clear();
  1112. }
  1113. break;
  1114. }
  1115. default:
  1116. llassert(false);
  1117. dst_image->clear();
  1118. }
  1119. if (maximum > minimum)
  1120. {
  1121. LL_FAST_TIMER(FTM_BUMP_SOURCE_RESCALE);
  1122. U8 bias_and_scale_lut[256];
  1123. F32 twice_one_over_range = 2.f / (maximum - minimum);
  1124. S32 i;
  1125. // Advantage: exaggerates the effect in midrange. Disadvantage: clamps
  1126. // at the extremes.
  1127. constexpr F32 ARTIFICIAL_SCALE = 2.f;
  1128. if (bump_code == BE_DARKNESS)
  1129. {
  1130. for (i = minimum; i <= maximum; ++i)
  1131. {
  1132. F32 minus_one_to_one = F32(maximum - i) *
  1133. twice_one_over_range - 1.f;
  1134. bias_and_scale_lut[i] = llclampb(ll_round(127 *
  1135. minus_one_to_one *
  1136. ARTIFICIAL_SCALE +
  1137. 128));
  1138. }
  1139. }
  1140. else
  1141. {
  1142. for (i = minimum; i <= maximum; ++i)
  1143. {
  1144. F32 minus_one_to_one = F32(i - minimum) *
  1145. twice_one_over_range - 1.f;
  1146. bias_and_scale_lut[i] = llclampb(ll_round(127 *
  1147. minus_one_to_one *
  1148. ARTIFICIAL_SCALE +
  1149. 128));
  1150. }
  1151. }
  1152. for (i = 0; i < dst_data_size; ++i)
  1153. {
  1154. dst_data[i] = bias_and_scale_lut[dst_data[i]];
  1155. }
  1156. }
  1157. //---------------------------------------------------
  1158. // Immediately assign bump to a smart pointer in case some local smart
  1159. // pointer accidentally releases it.
  1160. LLPointer<LLViewerTexture> bump = iter->second;
  1161. static LLCachedControl<bool> use_worker(gSavedSettings,
  1162. "GLWorkerUseForBumpmap");
  1163. bool can_queue = use_worker && LLImageGLThread::sEnabled && gMainloopWorkp;
  1164. if (!LLPipeline::sRenderDeferred)
  1165. {
  1166. LL_FAST_TIMER(FTM_BUMP_SOURCE_CREATE);
  1167. bump->setExplicitFormat(GL_ALPHA8, GL_ALPHA);
  1168. auto texq = can_queue ? gImageQueuep.lock() : nullptr;
  1169. if (texq)
  1170. {
  1171. // Dispatch creation to background thread
  1172. LLImageRaw* dst_ptr = dst_image.get();
  1173. LLViewerTexture* bump_ptr = bump.get();
  1174. dst_ptr->ref();
  1175. bump_ptr->ref();
  1176. texq->post([=]()
  1177. {
  1178. bump_ptr->createGLTexture(0, dst_ptr);
  1179. bump_ptr->unref();
  1180. dst_ptr->unref();
  1181. });
  1182. }
  1183. else
  1184. {
  1185. bump->createGLTexture(0, dst_image);
  1186. }
  1187. }
  1188. else // Convert to normal map
  1189. {
  1190. LLImageGL* img = bump->getGLImage();
  1191. LLImageRaw* dst_ptr = dst_image.get();
  1192. LLGLTexture* bump_ptr = bump.get();
  1193. dst_ptr->ref();
  1194. img->ref();
  1195. bump_ptr->ref();
  1196. auto create_func = [=]()
  1197. {
  1198. img->setUseMipMaps(true);
  1199. // Upload dst_image to GPU (greyscale in red channel)
  1200. img->setExplicitFormat(GL_RED, GL_RED);
  1201. bump_ptr->createGLTexture(0, dst_ptr);
  1202. dst_ptr->unref();
  1203. };
  1204. static LLCachedControl<F32> norm_scale(gSavedSettings,
  1205. "RenderNormalMapScale");
  1206. auto generate_func = [=]()
  1207. {
  1208. // Allocate an empty RGBA texture at "tex_name" the same size as
  1209. // bump. Note: bump will still point at GPU copy of dst_image.
  1210. bump_ptr->setExplicitFormat(GL_RGBA, GL_RGBA);
  1211. U32 tex_name;
  1212. img->createGLTexture(0, NULL, false, 0, true, &tex_name);
  1213. // Point render target at empty buffer
  1214. sRenderTarget.setColorAttachment(img, tex_name);
  1215. // Generate normal map in empty texture
  1216. {
  1217. sRenderTarget.bindTarget();
  1218. LLGLDepthTest depth(GL_FALSE);
  1219. LLGLDisable cull(GL_CULL_FACE);
  1220. LLGLDisable blend(GL_BLEND);
  1221. gGL.setColorMask(true, true);
  1222. gNormalMapGenProgram.bind();
  1223. static LLStaticHashedString sNormScale("norm_scale");
  1224. static LLStaticHashedString sStepX("stepX");
  1225. static LLStaticHashedString sStepY("stepY");
  1226. gNormalMapGenProgram.uniform1f(sNormScale, norm_scale);
  1227. gNormalMapGenProgram.uniform1f(sStepX,
  1228. 1.f / bump_ptr->getWidth());
  1229. gNormalMapGenProgram.uniform1f(sStepY,
  1230. 1.f / bump_ptr->getHeight());
  1231. gGL.getTexUnit(0)->bind(bump);
  1232. gGL.begin(LLRender::TRIANGLE_STRIP);
  1233. gGL.texCoord2f(0.f, 0.f);
  1234. gGL.vertex2f(0.f, 0.f);
  1235. gGL.texCoord2f(0.f, 1.f);
  1236. gGL.vertex2f(0.f, 1.f);
  1237. gGL.texCoord2f(1.f, 0.f);
  1238. gGL.vertex2f(1.f, 0.f);
  1239. gGL.texCoord2f(1.f, 1.f);
  1240. gGL.vertex2f(1.f, 1.f);
  1241. gGL.end(true);
  1242. gNormalMapGenProgram.unbind();
  1243. sRenderTarget.flush();
  1244. sRenderTarget.releaseColorAttachment();
  1245. }
  1246. // Point bump at normal map and free GPU copy of dst_image
  1247. img->syncTexName(tex_name);
  1248. // Generate mipmap
  1249. LLTexUnit* unit0 = gGL.getTexUnit(0);
  1250. unit0->bind(img);
  1251. glGenerateMipmap(GL_TEXTURE_2D);
  1252. unit0->disable();
  1253. bump_ptr->unref();
  1254. img->unref();
  1255. };
  1256. // If possible, dispatch the texture upload to the background thread,
  1257. // issue GPU commands to the generate normal map on the main thread.
  1258. if (!can_queue ||
  1259. !gMainloopWorkp->postTo(gImageQueuep, create_func, generate_func))
  1260. {
  1261. // If not possible or failed, immediately upload the texture and
  1262. // generate the normal map
  1263. {
  1264. LL_FAST_TIMER(FTM_BUMP_SOURCE_CREATE);
  1265. create_func();
  1266. }
  1267. {
  1268. LL_FAST_TIMER(FTM_BUMP_SOURCE_CREATE);
  1269. generate_func();
  1270. }
  1271. }
  1272. }
  1273. iter->second = std::move(bump); // Derefs (and deletes) old image
  1274. }
  1275. // For the EE renderer only
  1276. void LLDrawPoolBump::renderBump(U32 type, U32 mask)
  1277. {
  1278. if (!gPipeline.sCull)
  1279. {
  1280. // Paranoia (sCull != NULL needed for getRenderMap())
  1281. return;
  1282. }
  1283. LLVOAvatar* last_avatarp = NULL;
  1284. U64 last_hash = 0;
  1285. if (mRigged)
  1286. {
  1287. // Nudge type enum and include skinweights for rigged pass
  1288. ++type;
  1289. mask |= LLVertexBuffer::MAP_WEIGHT4;
  1290. }
  1291. LLCullResult::drawinfo_list_t& draw_list = gPipeline.getRenderMap(type);
  1292. for (U32 i = 0, count = draw_list.size(); i < count; )
  1293. {
  1294. LLDrawInfo* paramsp = draw_list[i++];
  1295. // Draw info cache prefetching optimization.
  1296. if (i < count)
  1297. {
  1298. _mm_prefetch((char*)draw_list[i]->mVertexBuffer.get(),
  1299. _MM_HINT_NTA);
  1300. if (i + 1 < count)
  1301. {
  1302. _mm_prefetch((char*)draw_list[i + 1], _MM_HINT_NTA);
  1303. }
  1304. }
  1305. if (!bindBumpMap(*paramsp))
  1306. {
  1307. continue;
  1308. }
  1309. if (mRigged && paramsp->mAvatar && paramsp->mSkinInfo &&
  1310. (paramsp->mAvatar != last_avatarp ||
  1311. paramsp->mSkinInfo->mHash != last_hash))
  1312. {
  1313. if (!uploadMatrixPalette(*paramsp))
  1314. {
  1315. continue;
  1316. }
  1317. last_avatarp = paramsp->mAvatar;
  1318. last_hash = paramsp->mSkinInfo->mHash;
  1319. }
  1320. pushBumpBatch(*paramsp, mask, false);
  1321. }
  1322. }
  1323. //virtual
  1324. void LLDrawPoolBump::pushBatch(LLDrawInfo& params, U32 mask, bool texture,
  1325. bool batch_textures)
  1326. {
  1327. if (gUsePBRShaders)
  1328. {
  1329. // In LL's PBR code, pushBatch() is not a virtual method any more (the
  1330. // LLDrawPoolBump override was renamed as a non virtual pushBumpBatch()
  1331. // method instead), so when pushBatch() gets called on a bump draw
  1332. // pool, we must re-route it to the underlying LLRenderPass:pushBatch()
  1333. // method. This makes it compatible at the API level for both EE and
  1334. // PBR despite the virtual/non-virtual difference. HB
  1335. LLRenderPass::pushBatch(params, mask, texture, batch_textures);
  1336. }
  1337. else
  1338. {
  1339. pushBumpBatch(params, mask, texture, batch_textures);
  1340. }
  1341. }
  1342. void LLDrawPoolBump::pushBumpBatch(LLDrawInfo& params, U32 mask, bool texture,
  1343. bool batch_textures)
  1344. {
  1345. applyModelMatrix(params);
  1346. bool tex_setup = false;
  1347. U32 count = 0;
  1348. if (batch_textures && (count = params.mTextureList.size()) > 1)
  1349. {
  1350. for (U32 i = 0; i < count; ++i)
  1351. {
  1352. const LLPointer<LLViewerTexture>& tex = params.mTextureList[i];
  1353. if (tex.notNull())
  1354. {
  1355. gGL.getTexUnit(i)->bindFast(tex);
  1356. }
  1357. }
  1358. }
  1359. else
  1360. {
  1361. // Not batching textures or batch has only 1 texture: might need a
  1362. // texture matrix
  1363. if (params.mTextureMatrix)
  1364. {
  1365. if (mShiny)
  1366. {
  1367. gGL.getTexUnit(0)->activate();
  1368. gGL.matrixMode(LLRender::MM_TEXTURE);
  1369. }
  1370. else
  1371. {
  1372. gGL.getTexUnit(0)->activate();
  1373. gGL.matrixMode(LLRender::MM_TEXTURE);
  1374. gGL.loadMatrix(params.mTextureMatrix->getF32ptr());
  1375. ++gPipeline.mTextureMatrixOps;
  1376. }
  1377. gGL.loadMatrix(params.mTextureMatrix->getF32ptr());
  1378. ++gPipeline.mTextureMatrixOps;
  1379. tex_setup = true;
  1380. }
  1381. if (mShiny && mShaderLevel > 1 && texture)
  1382. {
  1383. if (params.mTexture.notNull())
  1384. {
  1385. gGL.getTexUnit(sDiffuseChannel)->bindFast(params.mTexture);
  1386. }
  1387. else
  1388. {
  1389. gGL.getTexUnit(sDiffuseChannel)->unbind(LLTexUnit::TT_TEXTURE);
  1390. }
  1391. }
  1392. }
  1393. // Note: mask is ignored for the PBR renderer
  1394. params.mVertexBuffer->setBufferFast(mask);
  1395. params.mVertexBuffer->drawRange(LLRender::TRIANGLES, params.mStart,
  1396. params.mEnd, params.mCount,
  1397. params.mOffset);
  1398. if (tex_setup)
  1399. {
  1400. if (mShiny)
  1401. {
  1402. gGL.getTexUnit(0)->activate();
  1403. }
  1404. else
  1405. {
  1406. gGL.getTexUnit(0)->activate();
  1407. gGL.matrixMode(LLRender::MM_TEXTURE);
  1408. }
  1409. gGL.loadIdentity();
  1410. gGL.matrixMode(LLRender::MM_MODELVIEW);
  1411. }
  1412. }
  1413. // For the PBR renderer only
  1414. void LLDrawPoolBump::pushBumpBatches(U32 type)
  1415. {
  1416. if (!gPipeline.sCull)
  1417. {
  1418. // Paranoia (sCull != NULL needed for getRenderMap())
  1419. return;
  1420. }
  1421. if (mRigged)
  1422. {
  1423. // Nudge type enum and include skin weights for rigged pass.
  1424. ++type;
  1425. }
  1426. LLVOAvatar* last_avatarp = NULL;
  1427. U64 last_hash = 0;
  1428. LLCullResult::drawinfo_list_t& draw_list = gPipeline.getRenderMap(type);
  1429. for (U32 i = 0, count = draw_list.size(); i < count; )
  1430. {
  1431. LLDrawInfo* paramsp = draw_list[i++];
  1432. // Draw info cache prefetching optimization.
  1433. if (i < count)
  1434. {
  1435. _mm_prefetch((char*)draw_list[i]->mVertexBuffer.get(),
  1436. _MM_HINT_NTA);
  1437. if (i + 1 < count)
  1438. {
  1439. _mm_prefetch((char*)draw_list[i + 1], _MM_HINT_NTA);
  1440. }
  1441. }
  1442. if (!bindBumpMap(*paramsp))
  1443. {
  1444. continue;
  1445. }
  1446. if (mRigged && paramsp->mAvatar && paramsp->mSkinInfo &&
  1447. (paramsp->mAvatar != last_avatarp ||
  1448. paramsp->mSkinInfo->mHash != last_hash))
  1449. {
  1450. if (!uploadMatrixPalette(*paramsp))
  1451. {
  1452. continue;
  1453. }
  1454. last_avatarp = paramsp->mAvatar;
  1455. last_hash = paramsp->mSkinInfo->mHash;
  1456. }
  1457. pushBumpBatch(*paramsp, 0, false);
  1458. }
  1459. }
  1460. // Renders invisiprims
  1461. void LLDrawPoolInvisible::render(S32)
  1462. {
  1463. LL_FAST_TIMER(FTM_RENDER_INVISIBLE);
  1464. bool has_shaders = gPipeline.shadersLoaded();
  1465. if (has_shaders)
  1466. {
  1467. gOcclusionProgram.bind();
  1468. }
  1469. glStencilMask(0);
  1470. gGL.setColorMask(false, false);
  1471. pushBatches(PASS_INVISIBLE, VERTEX_DATA_MASK, false);
  1472. gGL.setColorMask(true, false); // false for alpha mask in direct rendering
  1473. glStencilMask(0xFFFFFFFF);
  1474. if (has_shaders)
  1475. {
  1476. gOcclusionProgram.unbind();
  1477. }
  1478. }
  1479. void LLDrawPoolInvisible::renderDeferred(S32 pass)
  1480. {
  1481. LL_FAST_TIMER(FTM_RENDER_INVISIBLE);
  1482. // *TODO: since we kept the stencil for EE, see if we can re-implement this
  1483. // in PBR rendering mode. HB
  1484. static LLCachedControl<bool> deferred_invisible(gSavedSettings,
  1485. "RenderDeferredInvisible");
  1486. if (!deferred_invisible)
  1487. {
  1488. // This MUST be called nevertheless to restore the proper color masks.
  1489. // HB
  1490. gGL.setColorMask(true, true);
  1491. return;
  1492. }
  1493. bool has_shaders = gPipeline.shadersLoaded();
  1494. if (has_shaders)
  1495. {
  1496. gOcclusionProgram.bind();
  1497. }
  1498. glStencilMask(0);
  1499. //glStencilOp(GL_ZERO, GL_KEEP, GL_REPLACE);
  1500. gGL.setColorMask(false, false);
  1501. pushBatches(PASS_INVISIBLE, VERTEX_DATA_MASK, false);
  1502. gGL.setColorMask(true, true); // true for alpha masking in deferred mode
  1503. //glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  1504. glStencilMask(0xFFFFFFFF);
  1505. if (has_shaders)
  1506. {
  1507. gOcclusionProgram.unbind();
  1508. }
  1509. }