lltexlayer.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  1. /**
  2. * @file lltexlayer.cpp
  3. * @brief A texture layer. Used for avatars.
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2010, 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 "linden_common.h"
  33. #include "lltexlayer.h"
  34. #include "imageids.h"
  35. #include "llavatarappearance.h"
  36. #include "llcrc.h"
  37. #include "lldir.h"
  38. #include "llimagej2c.h"
  39. #include "llimagetga.h"
  40. #include "llrenderutils.h"
  41. #include "lltexlayerparams.h"
  42. #include "lltexturemanagerbridge.h"
  43. #include "hbtracy.h"
  44. #include "llvertexbuffer.h"
  45. #include "llviewervisualparam.h"
  46. #include "llwearable.h"
  47. #include "llwearabledata.h"
  48. bool LLTexLayerSet::sHasCaches = false;
  49. // The two following booleans are set in llstartup.cpp after login, for OpenSim
  50. // specific features.
  51. // In SL, face wrinkles cannot be baked any more (the SSB code was not fixed
  52. // to support them), while in OpenSim, the viewer still bakes its own textures
  53. // and we got the fix for face wrinkles. HB
  54. //static
  55. bool LLTexLayerSet::sAllowFaceWrinkles = true;
  56. // Set to true to enable large viewer bakes (this is set via a per-account
  57. // setting, so that the user may enable it on a per-grid basis). HB
  58. bool LLTexLayerSetInfo::sUseLargeBakes = false;
  59. // Global
  60. LLTexLayerStaticImageList gTexLayerStaticImageList;
  61. using namespace LLAvatarAppearanceDefines;
  62. // Runway consolidate
  63. extern std::string self_av_string();
  64. class LLTexLayerInfo
  65. {
  66. friend class LLTexLayer;
  67. friend class LLTexLayerTemplate;
  68. friend class LLTexLayerInterface;
  69. public:
  70. LLTexLayerInfo();
  71. ~LLTexLayerInfo();
  72. bool parseXml(LLXmlTreeNode* node);
  73. bool createVisualParams(LLAvatarAppearance* appearance);
  74. LL_INLINE bool isUserSettable() { return mLocalTexture != -1; }
  75. LL_INLINE S32 getLocalTexture() const { return mLocalTexture; }
  76. LL_INLINE bool getOnlyAlpha() const { return mUseLocalTextureAlphaOnly; }
  77. LL_INLINE std::string getName() const { return mName; }
  78. private:
  79. LLTexLayerInterface::ERenderPass mRenderPass;
  80. std::string mGlobalColor;
  81. LLColor4 mFixedColor;
  82. S32 mLocalTexture;
  83. std::string mStaticImageFileName;
  84. bool mStaticImageIsMask;
  85. // Don't use masking. Just write RGBA into buffer
  86. bool mWriteAllChannels;
  87. // Ignore RGB channels from the input texture. Use alpha as a mask
  88. bool mUseLocalTextureAlphaOnly;
  89. bool mIsVisibilityMask;
  90. std::string mName;
  91. param_color_info_list_t mParamColorInfoList;
  92. param_alpha_info_list_t mParamAlphaInfoList;
  93. #if 0 // Not used
  94. typedef std::vector<std::pair<std::string, bool> > morph_name_list_t;
  95. morph_name_list_t mMorphNameList;
  96. #endif
  97. };
  98. //-----------------------------------------------------------------------------
  99. // LLTexLayerSetBuffer
  100. // The composite image that a LLViewerTexLayerSet writes to.
  101. // Each LLViewerTexLayerSet has one.
  102. //-----------------------------------------------------------------------------
  103. LLTexLayerSetBuffer::LLTexLayerSetBuffer(LLTexLayerSet* const owner)
  104. : mTexLayerSet(owner)
  105. {
  106. }
  107. void LLTexLayerSetBuffer::pushProjection() const
  108. {
  109. gGL.matrixMode(LLRender::MM_PROJECTION);
  110. gGL.pushMatrix();
  111. gGL.loadIdentity();
  112. gGL.ortho(0.f, getCompositeWidth(), 0.f, getCompositeHeight(), -1.f, 1.f);
  113. gGL.matrixMode(LLRender::MM_MODELVIEW);
  114. gGL.pushMatrix();
  115. gGL.loadIdentity();
  116. }
  117. void LLTexLayerSetBuffer::popProjection() const
  118. {
  119. gGL.matrixMode(LLRender::MM_PROJECTION);
  120. gGL.popMatrix();
  121. gGL.matrixMode(LLRender::MM_MODELVIEW);
  122. gGL.popMatrix();
  123. }
  124. //virtual
  125. void LLTexLayerSetBuffer::preRenderTexLayerSet()
  126. {
  127. // Set up an ortho projection
  128. pushProjection();
  129. }
  130. //virtual
  131. void LLTexLayerSetBuffer::postRenderTexLayerSet(bool success)
  132. {
  133. popProjection();
  134. }
  135. bool LLTexLayerSetBuffer::renderTexLayerSet()
  136. {
  137. // Default color mask for tex layer render
  138. gGL.setColorMask(true, true);
  139. bool success = true;
  140. gAlphaMaskProgram.bind();
  141. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  142. LLVertexBuffer::unbind();
  143. // Composite the color data
  144. LLGLSUIDefault gls_ui;
  145. success &= mTexLayerSet->render(getCompositeOriginX(), getCompositeOriginY(),
  146. getCompositeWidth(), getCompositeHeight());
  147. midRenderTexLayerSet(success);
  148. gAlphaMaskProgram.unbind();
  149. LLVertexBuffer::unbind();
  150. // Reset GL state
  151. gGL.setColorMask(true, true);
  152. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  153. return success;
  154. }
  155. //-----------------------------------------------------------------------------
  156. // LLTexLayerSetInfo
  157. // An ordered set of texture layers that get composited into a single texture.
  158. //-----------------------------------------------------------------------------
  159. LLTexLayerSetInfo::LLTexLayerSetInfo()
  160. : mBodyRegion(""),
  161. mWidth(512),
  162. mHeight(512),
  163. mClearAlpha(true)
  164. {
  165. }
  166. LLTexLayerSetInfo::~LLTexLayerSetInfo()
  167. {
  168. std::for_each(mLayerInfoList.begin(), mLayerInfoList.end(),
  169. DeletePointer());
  170. mLayerInfoList.clear();
  171. }
  172. bool LLTexLayerSetInfo::parseXml(LLXmlTreeNode* node)
  173. {
  174. llassert(node->hasName("layer_set"));
  175. if (!node->hasName("layer_set"))
  176. {
  177. return false;
  178. }
  179. // body_region
  180. static LLStdStringHandle body_region_string =
  181. LLXmlTree::addAttributeString("body_region");
  182. if (!node->getFastAttributeString(body_region_string, mBodyRegion))
  183. {
  184. llwarns << "<layer_set> is missing body_region attribute" << llendl;
  185. return false;
  186. }
  187. // width, height
  188. static LLStdStringHandle width_string =
  189. LLXmlTree::addAttributeString("width");
  190. if (!node->getFastAttributeS32(width_string, mWidth))
  191. {
  192. return false;
  193. }
  194. static LLStdStringHandle height_string =
  195. LLXmlTree::addAttributeString("height");
  196. if (!node->getFastAttributeS32(height_string, mHeight))
  197. {
  198. return false;
  199. }
  200. // *HACK: force the size down to 512x512 when not using large bakes. HB
  201. if (mWidth == 1024 && mHeight == 1024)
  202. {
  203. if (sUseLargeBakes)
  204. {
  205. llinfos << mBodyRegion << " bakes set to 1024x1024 pixels"
  206. << llendl;
  207. }
  208. else
  209. {
  210. mWidth = mHeight = 512;
  211. }
  212. }
  213. // Optional alpha component to apply after all compositing is complete.
  214. static LLStdStringHandle alpha_tga_file_string =
  215. LLXmlTree::addAttributeString("alpha_tga_file");
  216. node->getFastAttributeString(alpha_tga_file_string, mStaticAlphaFileName);
  217. static LLStdStringHandle clear_alpha_string =
  218. LLXmlTree::addAttributeString("clear_alpha");
  219. node->getFastAttributeBool(clear_alpha_string, mClearAlpha);
  220. // <layer>
  221. for (LLXmlTreeNode* child = node->getChildByName("layer");
  222. child; child = node->getNextNamedChild())
  223. {
  224. LLTexLayerInfo* info = new LLTexLayerInfo();
  225. if (!info->parseXml(child))
  226. {
  227. delete info;
  228. return false;
  229. }
  230. mLayerInfoList.push_back(info);
  231. }
  232. return true;
  233. }
  234. // creates visual params without generating layersets or layers
  235. void LLTexLayerSetInfo::createVisualParams(LLAvatarAppearance* appearance)
  236. {
  237. //layer_info_list_t mLayerInfoList;
  238. for (layer_info_list_t::iterator layer_iter = mLayerInfoList.begin(),
  239. end = mLayerInfoList.end();
  240. layer_iter != end; ++layer_iter)
  241. {
  242. LLTexLayerInfo* layer_info = *layer_iter;
  243. layer_info->createVisualParams(appearance);
  244. }
  245. }
  246. //-----------------------------------------------------------------------------
  247. // LLTexLayerSet
  248. // An ordered set of texture layers that get composited into a single texture.
  249. //-----------------------------------------------------------------------------
  250. LLTexLayerSet::LLTexLayerSet(LLAvatarAppearance* const appearance)
  251. : mAvatarAppearance(appearance),
  252. mIsVisible(true),
  253. mBakedTexIndex(LLAvatarAppearanceDefines::BAKED_HEAD),
  254. mInfo(NULL)
  255. {
  256. }
  257. //virtual
  258. LLTexLayerSet::~LLTexLayerSet()
  259. {
  260. deleteCaches();
  261. std::for_each(mLayerList.begin(), mLayerList.end(), DeletePointer());
  262. mLayerList.clear();
  263. std::for_each(mMaskLayerList.begin(), mMaskLayerList.end(),
  264. DeletePointer());
  265. mMaskLayerList.clear();
  266. }
  267. bool LLTexLayerSet::setInfo(const LLTexLayerSetInfo* info)
  268. {
  269. llassert(mInfo == NULL);
  270. mInfo = info;
  271. //mID = info->mID; // No ID
  272. mLayerList.reserve(info->mLayerInfoList.size());
  273. for (LLTexLayerSetInfo::layer_info_list_t::const_iterator
  274. iter = info->mLayerInfoList.begin(),
  275. end = info->mLayerInfoList.end();
  276. iter != end; ++iter)
  277. {
  278. LLTexLayerInterface* layer = NULL;
  279. if ((*iter)->isUserSettable())
  280. {
  281. layer = new LLTexLayerTemplate(this, getAvatarAppearance());
  282. }
  283. else
  284. {
  285. layer = new LLTexLayer(this);
  286. }
  287. // This is the first time this layer (of either type) is being created
  288. // - make sure you add the parameters to the avatar appearance.
  289. if (!layer->setInfo(*iter, NULL))
  290. {
  291. mInfo = NULL;
  292. return false;
  293. }
  294. if (!layer->isVisibilityMask())
  295. {
  296. mLayerList.push_back(layer);
  297. }
  298. else
  299. {
  300. mMaskLayerList.push_back(layer);
  301. }
  302. }
  303. requestUpdate();
  304. return true;
  305. }
  306. void LLTexLayerSet::deleteCaches()
  307. {
  308. for (layer_list_t::iterator iter = mLayerList.begin();
  309. iter != mLayerList.end(); ++iter)
  310. {
  311. LLTexLayerInterface* layer = *iter;
  312. layer->deleteCaches();
  313. }
  314. for (layer_list_t::iterator iter = mMaskLayerList.begin();
  315. iter != mMaskLayerList.end(); ++iter)
  316. {
  317. LLTexLayerInterface* layer = *iter;
  318. layer->deleteCaches();
  319. }
  320. }
  321. bool LLTexLayerSet::render(S32 x, S32 y, S32 width, S32 height)
  322. {
  323. mIsVisible = true;
  324. if (mMaskLayerList.size() > 0)
  325. {
  326. for (layer_list_t::iterator iter = mMaskLayerList.begin(),
  327. end = mMaskLayerList.end();
  328. iter != end; ++iter)
  329. {
  330. LLTexLayerInterface* layer = *iter;
  331. if (layer->isInvisibleAlphaMask())
  332. {
  333. mIsVisible = false;
  334. }
  335. }
  336. }
  337. LLGLSUIDefault gls_ui;
  338. LLGLDepthTest gls_depth(GL_FALSE, GL_FALSE);
  339. gGL.setColorMask(true, true);
  340. // Clear buffer area to ensure we don't pick up UI elements
  341. {
  342. gAlphaMaskProgram.setMinimumAlpha(0.f);
  343. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  344. gGL.color4f(0.f, 0.f, 0.f, 1.f);
  345. gl_rect_2d_simple(width, height);
  346. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  347. }
  348. bool success = true;
  349. if (mIsVisible)
  350. {
  351. // Composite color layers
  352. for (layer_list_t::iterator iter = mLayerList.begin(),
  353. end = mLayerList.end();
  354. iter != end; ++iter)
  355. {
  356. LLTexLayerInterface* layer = *iter;
  357. if (layer->getRenderPass() == LLTexLayer::RP_COLOR ||
  358. (sAllowFaceWrinkles &&
  359. layer->getRenderPass() == LLTexLayer::RP_BUMP))
  360. {
  361. success &= layer->render(x, y, width, height);
  362. }
  363. }
  364. renderAlphaMaskTextures(x, y, width, height, false);
  365. }
  366. else
  367. {
  368. gGL.flush();
  369. gGL.setSceneBlendType(LLRender::BT_REPLACE);
  370. gAlphaMaskProgram.setMinimumAlpha(0.f);
  371. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  372. gGL.color4f(0.f, 0.f, 0.f, 0.f);
  373. gl_rect_2d_simple(width, height);
  374. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  375. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  376. }
  377. stop_glerror();
  378. return success;
  379. }
  380. bool LLTexLayerSet::isBodyRegion(const std::string& region) const
  381. {
  382. return mInfo->mBodyRegion == region;
  383. }
  384. const std::string LLTexLayerSet::getBodyRegionName() const
  385. {
  386. return mInfo->mBodyRegion;
  387. }
  388. void LLTexLayerSet::destroyComposite()
  389. {
  390. if (mComposite)
  391. {
  392. mComposite = NULL;
  393. }
  394. }
  395. LLTexLayerSetBuffer* LLTexLayerSet::getComposite()
  396. {
  397. if (!mComposite)
  398. {
  399. createComposite();
  400. }
  401. return mComposite;
  402. }
  403. const LLTexLayerSetBuffer* LLTexLayerSet::getComposite() const
  404. {
  405. return mComposite;
  406. }
  407. void LLTexLayerSet::gatherMorphMaskAlpha(U8* data, S32 origin_x, S32 origin_y,
  408. S32 width, S32 height)
  409. {
  410. LL_TRACY_TIMER(TRC_GATHER_MORPH_MASK_ALPHA);
  411. memset(data, 255, width * height);
  412. for (layer_list_t::iterator iter = mLayerList.begin(),
  413. end = mLayerList.end();
  414. iter != end; ++iter)
  415. {
  416. LLTexLayerInterface* layer = *iter;
  417. layer->gatherAlphaMasks(data, origin_x, origin_y, width, height);
  418. }
  419. // Set alpha back to that of our alpha masks.
  420. renderAlphaMaskTextures(origin_x, origin_y, width, height, true);
  421. }
  422. void LLTexLayerSet::renderAlphaMaskTextures(S32 x, S32 y, S32 width,
  423. S32 height, bool forceClear)
  424. {
  425. LL_TRACY_TIMER(TRC_RENDER_ALPHA_MASK_TEXTURES);
  426. const LLTexLayerSetInfo* info = getInfo();
  427. gGL.setColorMask(false, true);
  428. gGL.setSceneBlendType(LLRender::BT_REPLACE);
  429. LLTexUnit* unit0 = gGL.getTexUnit(0);
  430. // (Optionally) replace alpha with a single component image from a tga file.
  431. if (!info->mStaticAlphaFileName.empty())
  432. {
  433. LLGLTexture* texp =
  434. gTexLayerStaticImageList.getTexture(info->mStaticAlphaFileName,
  435. true);
  436. if (texp)
  437. {
  438. LLGLSUIDefault gls_ui;
  439. unit0->bind(texp);
  440. gl_rect_2d_simple_tex(width, height);
  441. }
  442. }
  443. else if (forceClear || info->mClearAlpha || mMaskLayerList.size() > 0)
  444. {
  445. gAlphaMaskProgram.setMinimumAlpha(0.f);
  446. unit0->unbind(LLTexUnit::TT_TEXTURE);
  447. gGL.color4f(0.f, 0.f, 0.f, 1.f);
  448. gl_rect_2d_simple(width, height);
  449. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  450. }
  451. // (Optional) Mask out part of the baked texture with alpha masks; will
  452. // still have an effect even if mClearAlpha is set or the alpha component
  453. // was replaced.
  454. if (mMaskLayerList.size() > 0)
  455. {
  456. gGL.setSceneBlendType(LLRender::BT_MULT_ALPHA);
  457. for (layer_list_t::iterator iter = mMaskLayerList.begin(),
  458. end = mMaskLayerList.end();
  459. iter != end; ++iter)
  460. {
  461. LLTexLayerInterface* layer = *iter;
  462. layer->blendAlphaTexture(x, y, width, height);
  463. }
  464. }
  465. unit0->unbind(LLTexUnit::TT_TEXTURE);
  466. gGL.setColorMask(true, true);
  467. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  468. }
  469. void LLTexLayerSet::applyMorphMask(U8* tex_data, S32 width, S32 height,
  470. S32 num_components)
  471. {
  472. mAvatarAppearance->applyMorphMask(tex_data, width, height, num_components,
  473. mBakedTexIndex);
  474. }
  475. bool LLTexLayerSet::isMorphValid() const
  476. {
  477. for (layer_list_t::const_iterator iter = mLayerList.begin(),
  478. end = mLayerList.end();
  479. iter != end; ++iter)
  480. {
  481. const LLTexLayerInterface* layer = *iter;
  482. if (layer && !layer->isMorphValid())
  483. {
  484. return false;
  485. }
  486. }
  487. return true;
  488. }
  489. void LLTexLayerSet::invalidateMorphMasks()
  490. {
  491. for (layer_list_t::iterator iter = mLayerList.begin(),
  492. end = mLayerList.end();
  493. iter != end; ++iter)
  494. {
  495. LLTexLayerInterface* layer = *iter;
  496. if (layer)
  497. {
  498. layer->invalidateMorphMasks();
  499. }
  500. }
  501. }
  502. //-----------------------------------------------------------------------------
  503. // LLTexLayerInfo
  504. //-----------------------------------------------------------------------------
  505. LLTexLayerInfo::LLTexLayerInfo()
  506. : mWriteAllChannels(false),
  507. mRenderPass(LLTexLayer::RP_COLOR),
  508. mFixedColor(0.f, 0.f, 0.f, 0.f),
  509. mLocalTexture(-1),
  510. mStaticImageIsMask(false),
  511. mUseLocalTextureAlphaOnly(false),
  512. mIsVisibilityMask(false)
  513. {
  514. }
  515. LLTexLayerInfo::~LLTexLayerInfo()
  516. {
  517. std::for_each(mParamColorInfoList.begin(), mParamColorInfoList.end(),
  518. DeletePointer());
  519. mParamColorInfoList.clear();
  520. std::for_each(mParamAlphaInfoList.begin(), mParamAlphaInfoList.end(),
  521. DeletePointer());
  522. mParamAlphaInfoList.clear();
  523. }
  524. bool LLTexLayerInfo::parseXml(LLXmlTreeNode* node)
  525. {
  526. llassert(node->hasName("layer"));
  527. // name attribute
  528. static LLStdStringHandle name_string =
  529. LLXmlTree::addAttributeString("name");
  530. if (!node->getFastAttributeString(name_string, mName))
  531. {
  532. return false;
  533. }
  534. static LLStdStringHandle write_all_channels_string =
  535. LLXmlTree::addAttributeString("write_all_channels");
  536. node->getFastAttributeBool(write_all_channels_string, mWriteAllChannels);
  537. std::string render_pass_name;
  538. static LLStdStringHandle render_pass_string =
  539. LLXmlTree::addAttributeString("render_pass");
  540. if (node->getFastAttributeString(render_pass_string, render_pass_name))
  541. {
  542. if (render_pass_name == "bump")
  543. {
  544. mRenderPass = LLTexLayer::RP_BUMP;
  545. }
  546. }
  547. // Note: layers can have either a "global_color" attrib, a "fixed_color"
  548. // attrib, or a <param_color> child.
  549. // global color attribute (optional)
  550. static LLStdStringHandle global_color_string =
  551. LLXmlTree::addAttributeString("global_color");
  552. node->getFastAttributeString(global_color_string, mGlobalColor);
  553. // Visibility mask (optional)
  554. bool is_visibility;
  555. static LLStdStringHandle visibility_mask_string =
  556. LLXmlTree::addAttributeString("visibility_mask");
  557. if (node->getFastAttributeBool(visibility_mask_string, is_visibility))
  558. {
  559. mIsVisibilityMask = is_visibility;
  560. }
  561. // color attribute (optional)
  562. static LLStdStringHandle fixed_color_string =
  563. LLXmlTree::addAttributeString("fixed_color");
  564. LLColor4U color4u;
  565. if (node->getFastAttributeColor4U(fixed_color_string, color4u))
  566. {
  567. mFixedColor.set(color4u);
  568. }
  569. // <texture> optional sub-element
  570. static LLStdStringHandle tga_file_string =
  571. LLXmlTree::addAttributeString("tga_file");
  572. static LLStdStringHandle local_texture_string =
  573. LLXmlTree::addAttributeString("local_texture");
  574. static LLStdStringHandle file_is_mask_string =
  575. LLXmlTree::addAttributeString("file_is_mask");
  576. static LLStdStringHandle local_texture_alpha_only_string =
  577. LLXmlTree::addAttributeString("local_texture_alpha_only");
  578. for (LLXmlTreeNode* texture_node = node->getChildByName("texture");
  579. texture_node; texture_node = node->getNextNamedChild())
  580. {
  581. std::string local_texture_name;
  582. if (texture_node->getFastAttributeString(tga_file_string,
  583. mStaticImageFileName))
  584. {
  585. texture_node->getFastAttributeBool(file_is_mask_string,
  586. mStaticImageIsMask);
  587. }
  588. else if (texture_node->getFastAttributeString(local_texture_string,
  589. local_texture_name))
  590. {
  591. texture_node->getFastAttributeBool(local_texture_alpha_only_string,
  592. mUseLocalTextureAlphaOnly);
  593. mLocalTexture = TEX_NUM_INDICES;
  594. for (LLAvatarAppearanceDictionary::Textures::const_iterator
  595. iter = gAvatarAppDictp->getTextures().begin(),
  596. end = gAvatarAppDictp->getTextures().end();
  597. iter != end; ++iter)
  598. {
  599. const LLAvatarAppearanceDictionary::TextureEntry* texdict =
  600. iter->second;
  601. if (local_texture_name == texdict->mName)
  602. {
  603. mLocalTexture = iter->first;
  604. break;
  605. }
  606. }
  607. if (mLocalTexture == TEX_NUM_INDICES)
  608. {
  609. llwarns << "<texture> element has invalid local_texture attribute: "
  610. << mName << " " << local_texture_name << llendl;
  611. return false;
  612. }
  613. }
  614. else
  615. {
  616. llwarns << "<texture> element is missing a required attribute: "
  617. << mName << llendl;
  618. return false;
  619. }
  620. }
  621. #if 0 // Not used
  622. static LLStdStringHandle morph_name_string =
  623. LLXmlTree::addAttributeString("morph_name");
  624. static LLStdStringHandle invert_string =
  625. LLXmlTree::addAttributeString("invert");
  626. for (LLXmlTreeNode* mask_node = node->getChildByName("morph_mask");
  627. mask_node; mask_node = node->getNextNamedChild())
  628. {
  629. std::string morph_name;
  630. if (mask_node->getFastAttributeString(morph_name_string, morph_name))
  631. {
  632. bool invert = false;
  633. mask_node->getFastAttributeBool(invert_string, invert);
  634. mMorphNameList.emplace_back(morph_name, invert);
  635. }
  636. }
  637. #endif
  638. // <param> optional sub-element (color or alpha params)
  639. for (LLXmlTreeNode* child = node->getChildByName("param");
  640. child; child = node->getNextNamedChild())
  641. {
  642. if (child->getChildByName("param_color"))
  643. {
  644. // <param><param_color/></param>
  645. LLTexLayerParamColorInfo* info = new LLTexLayerParamColorInfo();
  646. if (!info->parseXml(child))
  647. {
  648. delete info;
  649. return false;
  650. }
  651. mParamColorInfoList.push_back(info);
  652. }
  653. else if (child->getChildByName("param_alpha"))
  654. {
  655. // <param><param_alpha/></param>
  656. LLTexLayerParamAlphaInfo* info = new LLTexLayerParamAlphaInfo();
  657. if (!info->parseXml(child))
  658. {
  659. delete info;
  660. return false;
  661. }
  662. mParamAlphaInfoList.push_back(info);
  663. }
  664. }
  665. return true;
  666. }
  667. bool LLTexLayerInfo::createVisualParams(LLAvatarAppearance* appearance)
  668. {
  669. bool success = true;
  670. for (param_color_info_list_t::iterator
  671. color_info_iter = mParamColorInfoList.begin(),
  672. end = mParamColorInfoList.end();
  673. color_info_iter != end; ++color_info_iter)
  674. {
  675. LLTexLayerParamColorInfo* color_info = *color_info_iter;
  676. LLTexLayerParamColor* param_color = new LLTexLayerParamColor(appearance);
  677. if (!param_color->setInfo(color_info, true))
  678. {
  679. llwarns << "NULL TexLayer Color Param could not be added to visual param list. Deleting."
  680. << llendl;
  681. delete param_color;
  682. success = false;
  683. }
  684. }
  685. for (param_alpha_info_list_t::iterator
  686. alpha_info_iter = mParamAlphaInfoList.begin(),
  687. end = mParamAlphaInfoList.end();
  688. alpha_info_iter != end; ++alpha_info_iter)
  689. {
  690. LLTexLayerParamAlphaInfo* alpha_info = *alpha_info_iter;
  691. LLTexLayerParamAlpha* param_alpha = new LLTexLayerParamAlpha(appearance);
  692. if (!param_alpha->setInfo(alpha_info, true))
  693. {
  694. llwarns << "NULL TexLayer Alpha Param could not be added to visual param list. Deleting."
  695. << llendl;
  696. delete param_alpha;
  697. success = false;
  698. }
  699. }
  700. return success;
  701. }
  702. LLTexLayerInterface::LLTexLayerInterface(LLTexLayerSet* const layer_set)
  703. : mTexLayerSet(layer_set),
  704. mInfo(NULL),
  705. mMorphMasksValid(false),
  706. mHasMorph(false)
  707. {
  708. }
  709. LLTexLayerInterface::LLTexLayerInterface(const LLTexLayerInterface& layer,
  710. LLWearable* wearable)
  711. : mTexLayerSet(layer.mTexLayerSet),
  712. mInfo(NULL)
  713. {
  714. // Do not add visual params for cloned layers
  715. setInfo(layer.getInfo(), wearable);
  716. mHasMorph = layer.mHasMorph;
  717. }
  718. // This sets mInfo and calls initialization functions
  719. bool LLTexLayerInterface::setInfo(const LLTexLayerInfo* info,
  720. LLWearable* wearable)
  721. {
  722. // setInfo should only be called once. Code is not robust enough to handle
  723. // redefinition of a texlayer.
  724. // Not a critical warning, but could be useful for debugging later issues. -Nyx
  725. if (mInfo)
  726. {
  727. llwarns << "mInfo != NULL" << llendl;
  728. }
  729. mInfo = info;
  730. //mID = info->mID; // No ID
  731. mParamColorList.reserve(mInfo->mParamColorInfoList.size());
  732. for (param_color_info_list_t::const_iterator
  733. iter = mInfo->mParamColorInfoList.begin(),
  734. end = mInfo->mParamColorInfoList.end();
  735. iter != end; ++iter)
  736. {
  737. LLTexLayerParamColor* param_color;
  738. if (!wearable)
  739. {
  740. param_color = new LLTexLayerParamColor(this);
  741. if (!param_color->setInfo(*iter, true))
  742. {
  743. mInfo = NULL;
  744. return false;
  745. }
  746. }
  747. else
  748. {
  749. param_color = (LLTexLayerParamColor*)wearable->getVisualParam((*iter)->getID());
  750. if (!param_color)
  751. {
  752. mInfo = NULL;
  753. return false;
  754. }
  755. }
  756. mParamColorList.push_back(param_color);
  757. }
  758. mParamAlphaList.reserve(mInfo->mParamAlphaInfoList.size());
  759. for (param_alpha_info_list_t::const_iterator
  760. iter = mInfo->mParamAlphaInfoList.begin(),
  761. end = mInfo->mParamAlphaInfoList.end();
  762. iter != end; ++iter)
  763. {
  764. LLTexLayerParamAlpha* param_alpha;
  765. if (!wearable)
  766. {
  767. param_alpha = new LLTexLayerParamAlpha(this);
  768. if (!param_alpha->setInfo(*iter, true))
  769. {
  770. mInfo = NULL;
  771. return false;
  772. }
  773. }
  774. else
  775. {
  776. param_alpha =
  777. (LLTexLayerParamAlpha*)wearable->getVisualParam((*iter)->getID());
  778. if (!param_alpha)
  779. {
  780. mInfo = NULL;
  781. return false;
  782. }
  783. }
  784. mParamAlphaList.push_back(param_alpha);
  785. }
  786. return true;
  787. }
  788. //virtual
  789. void LLTexLayerInterface::requestUpdate()
  790. {
  791. mTexLayerSet->requestUpdate();
  792. }
  793. const std::string& LLTexLayerInterface::getName() const
  794. {
  795. return mInfo->mName;
  796. }
  797. ETextureIndex LLTexLayerInterface::getLocalTextureIndex() const
  798. {
  799. return (ETextureIndex)mInfo->mLocalTexture;
  800. }
  801. LLWearableType::EType LLTexLayerInterface::getWearableType() const
  802. {
  803. ETextureIndex te = getLocalTextureIndex();
  804. if (TEX_INVALID == te)
  805. {
  806. LLWearableType::EType type = LLWearableType::WT_INVALID;
  807. LLWearableType::EType new_type;
  808. for (param_color_list_t::const_iterator
  809. color_iter = mParamColorList.begin(),
  810. color_end = mParamColorList.end();
  811. color_iter != color_end; ++color_iter)
  812. {
  813. LLTexLayerParamColor* param = *color_iter;
  814. if (param)
  815. {
  816. new_type = (LLWearableType::EType)param->getWearableType();
  817. if (new_type != LLWearableType::WT_INVALID && new_type != type)
  818. {
  819. if (type != LLWearableType::WT_INVALID)
  820. {
  821. return LLWearableType::WT_INVALID;
  822. }
  823. type = new_type;
  824. }
  825. }
  826. }
  827. for (param_alpha_list_t::const_iterator
  828. alpha_iter = mParamAlphaList.begin(),
  829. alpha_end = mParamAlphaList.end();
  830. alpha_iter != alpha_end; alpha_iter++)
  831. {
  832. LLTexLayerParamAlpha* param = *alpha_iter;
  833. if (param)
  834. {
  835. new_type = (LLWearableType::EType)param->getWearableType();
  836. if (new_type != LLWearableType::WT_INVALID && new_type != type)
  837. {
  838. if (type != LLWearableType::WT_INVALID)
  839. {
  840. return LLWearableType::WT_INVALID;
  841. }
  842. type = new_type;
  843. }
  844. }
  845. }
  846. return type;
  847. }
  848. return LLAvatarAppearanceDictionary::getTEWearableType(te);
  849. }
  850. LLTexLayerInterface::ERenderPass LLTexLayerInterface::getRenderPass() const
  851. {
  852. return mInfo->mRenderPass;
  853. }
  854. const std::string& LLTexLayerInterface::getGlobalColor() const
  855. {
  856. return mInfo->mGlobalColor;
  857. }
  858. bool LLTexLayerInterface::isVisibilityMask() const
  859. {
  860. return mInfo->mIsVisibilityMask;
  861. }
  862. void LLTexLayerInterface::invalidateMorphMasks()
  863. {
  864. mMorphMasksValid = false;
  865. }
  866. LLViewerVisualParam* LLTexLayerInterface::getVisualParamPtr(S32 index) const
  867. {
  868. LLViewerVisualParam* paramp = NULL;
  869. for (param_color_list_t::const_iterator it = mParamColorList.begin(),
  870. end = mParamColorList.end();
  871. it != end && !paramp; ++it)
  872. {
  873. if ((*it)->getID() == index)
  874. {
  875. paramp = *it;
  876. }
  877. }
  878. for (param_alpha_list_t::const_iterator it = mParamAlphaList.begin(),
  879. end = mParamAlphaList.end();
  880. it != end && !paramp; ++it)
  881. {
  882. if ((*it)->getID() == index)
  883. {
  884. paramp = *it;
  885. }
  886. }
  887. return paramp;
  888. }
  889. //-----------------------------------------------------------------------------
  890. // LLTexLayer
  891. // A single texture layer, consisting of:
  892. // * color, consisting of either
  893. // * one or more color parameters (weighted colors)
  894. // * a reference to a global color
  895. // * a fixed color with non-zero alpha
  896. // * opaque white (the default)
  897. // * (optional) a texture defined by either
  898. // * a GUID
  899. // * a texture entry index (TE)
  900. // * (optional) one or more alpha parameters (weighted alpha textures)
  901. //-----------------------------------------------------------------------------
  902. LLTexLayer::LLTexLayer(LLTexLayerSet* const layer_setp)
  903. : LLTexLayerInterface(layer_setp),
  904. mLocalTextureObject(NULL)
  905. {
  906. }
  907. LLTexLayer::LLTexLayer(const LLTexLayer& layer, LLWearable* wearablep)
  908. : LLTexLayerInterface(layer, wearablep),
  909. mLocalTextureObject(NULL)
  910. {
  911. }
  912. LLTexLayer::LLTexLayer(const LLTexLayerTemplate& layer_template,
  913. LLLocalTextureObject* ltop,
  914. LLWearable* wearablep)
  915. : LLTexLayerInterface(layer_template, wearablep),
  916. mLocalTextureObject(ltop)
  917. {
  918. }
  919. LLTexLayer::~LLTexLayer()
  920. {
  921. #if 0 // mParamAlphaList and mParamColorList are LLViewerVisualParams and get
  922. // deleted with ~LLCharacter()
  923. std::for_each(mParamAlphaList.begin(), mParamAlphaList.end(),
  924. DeletePointer());
  925. mParamAlphaList.clear();
  926. std::for_each(mParamColorList.begin(), mParamColorList.end(),
  927. DeletePointer());
  928. mParamColorList.clear();
  929. #endif
  930. for (alpha_cache_t::iterator iter = mAlphaCache.begin(),
  931. end = mAlphaCache.end();
  932. iter != end; ++iter)
  933. {
  934. U8* alpha_data = iter->second;
  935. delete[] alpha_data;
  936. }
  937. }
  938. void LLTexLayer::asLLSD(LLSD& sd) const
  939. {
  940. // *TODO: Finish
  941. sd["id"] = getUUID();
  942. }
  943. bool LLTexLayer::setInfo(const LLTexLayerInfo* info, LLWearable* wearable)
  944. {
  945. return LLTexLayerInterface::setInfo(info, wearable);
  946. }
  947. //static
  948. void LLTexLayer::calculateTexLayerColor(const param_color_list_t& param_list,
  949. LLColor4& net_color)
  950. {
  951. for (param_color_list_t::const_iterator iter = param_list.begin(),
  952. end = param_list.end();
  953. iter != end; ++iter)
  954. {
  955. const LLTexLayerParamColor* param = *iter;
  956. LLColor4 param_net = param->getNetColor();
  957. const LLTexLayerParamColorInfo* info =
  958. (LLTexLayerParamColorInfo*)param->getInfo();
  959. switch (info->getOperation())
  960. {
  961. case LLTexLayerParamColor::OP_ADD:
  962. net_color += param_net;
  963. break;
  964. case LLTexLayerParamColor::OP_MULTIPLY:
  965. net_color = net_color * param_net;
  966. break;
  967. case LLTexLayerParamColor::OP_BLEND:
  968. net_color = lerp(net_color, param_net, param->getWeight());
  969. break;
  970. default:
  971. llassert(0);
  972. break;
  973. }
  974. }
  975. net_color.clamp();
  976. }
  977. //virtual
  978. void LLTexLayer::deleteCaches()
  979. {
  980. // Only need to delete caches for alpha params. Color params don't hold
  981. // extra memory
  982. for (param_alpha_list_t::iterator iter = mParamAlphaList.begin(),
  983. end = mParamAlphaList.end();
  984. iter != end; ++iter)
  985. {
  986. LLTexLayerParamAlpha* param = *iter;
  987. param->deleteCaches();
  988. }
  989. }
  990. bool LLTexLayer::render(S32 x, S32 y, S32 width, S32 height)
  991. {
  992. LLColor4 net_color;
  993. bool color_specified = findNetColor(&net_color);
  994. if (mTexLayerSet->getAvatarAppearance()->mIsDummy)
  995. {
  996. color_specified = true;
  997. net_color = LLAvatarAppearance::getDummyColor();
  998. }
  999. bool success = true;
  1000. // If you cannot see the layer, do not render it.
  1001. if (is_approx_zero(net_color.mV[VW]))
  1002. {
  1003. return success;
  1004. }
  1005. bool alpha_mask_specified = false;
  1006. param_alpha_list_t::const_iterator iter = mParamAlphaList.begin();
  1007. if (iter != mParamAlphaList.end())
  1008. {
  1009. // If we have alpha masks, but we are skipping all of them, skip the
  1010. // whole layer. However, we cannot do this optimization if we have
  1011. // morph masks that need updating.
  1012. #if 0
  1013. if (!mHasMorph)
  1014. {
  1015. bool skip_layer = true;
  1016. while (iter != mParamAlphaList.end())
  1017. {
  1018. const LLTexLayerParamAlpha* paramp = *iter;
  1019. if (!paramp->getSkip())
  1020. {
  1021. skip_layer = false;
  1022. break;
  1023. }
  1024. ++iter;
  1025. }
  1026. if (skip_layer)
  1027. {
  1028. return success;
  1029. }
  1030. }
  1031. #endif
  1032. renderMorphMasks(x, y, width, height, net_color, true);
  1033. alpha_mask_specified = true;
  1034. gGL.blendFunc(LLRender::BF_DEST_ALPHA,
  1035. LLRender::BF_ONE_MINUS_DEST_ALPHA);
  1036. }
  1037. const LLTexLayerInfo* infop = getInfo();
  1038. bool use_alpha_only = infop->mUseLocalTextureAlphaOnly;
  1039. gGL.color4fv(net_color.mV);
  1040. bool needs_mask_restore = false;
  1041. if (infop->mWriteAllChannels)
  1042. {
  1043. gGL.setSceneBlendType(LLRender::BT_REPLACE);
  1044. }
  1045. else if (use_alpha_only && LLTexLayerSet::sAllowFaceWrinkles)
  1046. {
  1047. // Use the alpha channel only
  1048. gGL.setColorMask(false, true);
  1049. needs_mask_restore = true;
  1050. }
  1051. if (infop->mLocalTexture != -1 && !use_alpha_only)
  1052. {
  1053. LLGLTexture* texp = NULL;
  1054. if (mLocalTextureObject && mLocalTextureObject->getImage())
  1055. {
  1056. texp = mLocalTextureObject->getImage();
  1057. if (mLocalTextureObject->getID() == IMG_DEFAULT_AVATAR)
  1058. {
  1059. texp = NULL;
  1060. }
  1061. }
  1062. else
  1063. {
  1064. llinfos << "LTO not defined or image not defined: "
  1065. << infop->getLocalTexture() << " lto: "
  1066. << mLocalTextureObject << llendl;
  1067. }
  1068. if (texp)
  1069. {
  1070. bool no_alpha_test = infop->mWriteAllChannels;
  1071. if (no_alpha_test)
  1072. {
  1073. gAlphaMaskProgram.setMinimumAlpha(0.f);
  1074. }
  1075. LLTexUnit::eTextureAddressMode old_mode = texp->getAddressMode();
  1076. LLTexUnit* unit0 = gGL.getTexUnit(0);
  1077. unit0->bind(texp);
  1078. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  1079. gl_rect_2d_simple_tex(width, height);
  1080. unit0->setTextureAddressMode(old_mode);
  1081. unit0->unbind(LLTexUnit::TT_TEXTURE);
  1082. if (no_alpha_test)
  1083. {
  1084. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  1085. }
  1086. }
  1087. }
  1088. if (!infop->mStaticImageFileName.empty())
  1089. {
  1090. LLGLTexture* texp =
  1091. gTexLayerStaticImageList.getTexture(infop->mStaticImageFileName,
  1092. infop->mStaticImageIsMask);
  1093. if (texp)
  1094. {
  1095. LLTexUnit* unit0 = gGL.getTexUnit(0);
  1096. unit0->bind(texp);
  1097. gl_rect_2d_simple_tex(width, height);
  1098. unit0->unbind(LLTexUnit::TT_TEXTURE);
  1099. }
  1100. else
  1101. {
  1102. success = false;
  1103. }
  1104. }
  1105. if (color_specified && infop->mStaticImageFileName.empty() &&
  1106. (infop->mLocalTexture == -1 || use_alpha_only))
  1107. {
  1108. gAlphaMaskProgram.setMinimumAlpha(0.000f);
  1109. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1110. gGL.color4fv(net_color.mV);
  1111. gl_rect_2d_simple(width, height);
  1112. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  1113. }
  1114. if (alpha_mask_specified || infop->mWriteAllChannels)
  1115. {
  1116. // Restore standard blend func value
  1117. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  1118. }
  1119. if (needs_mask_restore)
  1120. {
  1121. // Restore color + alpha mode.
  1122. gGL.setColorMask(true, true);
  1123. }
  1124. stop_glerror();
  1125. if (!success)
  1126. {
  1127. llinfos << "Partial render for: " << infop->mName << llendl;
  1128. }
  1129. return success;
  1130. }
  1131. const U8* LLTexLayer::getAlphaData() const
  1132. {
  1133. LLCRC alpha_mask_crc;
  1134. const LLUUID& id = getUUID();
  1135. alpha_mask_crc.update((U8*)(&id.mData), UUID_BYTES);
  1136. for (param_alpha_list_t::const_iterator
  1137. iter = mParamAlphaList.begin(),
  1138. end = mParamAlphaList.end(); iter != end; ++iter)
  1139. {
  1140. const LLTexLayerParamAlpha* paramp = *iter;
  1141. // MULTI-WEARABLE: verify visual parameters used here
  1142. F32 param_weight = paramp->getWeight();
  1143. alpha_mask_crc.update((U8*)&param_weight, sizeof(F32));
  1144. }
  1145. U32 cache_index = alpha_mask_crc.getCRC();
  1146. alpha_cache_t::const_iterator iter2 = mAlphaCache.find(cache_index);
  1147. return iter2 != mAlphaCache.end() ? iter2->second : 0;
  1148. }
  1149. bool LLTexLayer::findNetColor(LLColor4* colorp) const
  1150. {
  1151. // Color is either:
  1152. // * one or more color parameters (weighted colors, which may make use of
  1153. // a global color or fixed color)
  1154. // * a reference to a global color
  1155. // * a fixed color with non-zero alpha
  1156. // * opaque white (the default)
  1157. const LLTexLayerInfo* infop = getInfo();
  1158. if (!mParamColorList.empty())
  1159. {
  1160. if (!getGlobalColor().empty())
  1161. {
  1162. colorp->set(mTexLayerSet->getAvatarAppearance()->getGlobalColor(infop->mGlobalColor));
  1163. }
  1164. else if (infop->mFixedColor.mV[VW])
  1165. {
  1166. colorp->set(infop->mFixedColor);
  1167. }
  1168. else
  1169. {
  1170. colorp->set(0.f, 0.f, 0.f, 0.f);
  1171. }
  1172. calculateTexLayerColor(mParamColorList, *colorp);
  1173. return true;
  1174. }
  1175. if (!getGlobalColor().empty())
  1176. {
  1177. colorp->set(mTexLayerSet->getAvatarAppearance()->getGlobalColor(getGlobalColor()));
  1178. return true;
  1179. }
  1180. if (infop->mFixedColor.mV[VW])
  1181. {
  1182. colorp->set(infop->mFixedColor);
  1183. return true;
  1184. }
  1185. colorp->setToWhite();
  1186. return false; // No need to draw a separate colored polygon
  1187. }
  1188. bool LLTexLayer::blendAlphaTexture(S32 x, S32 y, S32 width, S32 height)
  1189. {
  1190. bool success = true;
  1191. const LLTexLayerInfo* infop = getInfo();
  1192. if (!infop->mStaticImageFileName.empty())
  1193. {
  1194. LLGLTexture* texp =
  1195. gTexLayerStaticImageList.getTexture(infop->mStaticImageFileName,
  1196. infop->mStaticImageIsMask);
  1197. if (texp)
  1198. {
  1199. gAlphaMaskProgram.setMinimumAlpha(0.f);
  1200. LLTexUnit* unit0 = gGL.getTexUnit(0);
  1201. unit0->bind(texp);
  1202. gl_rect_2d_simple_tex(width, height);
  1203. unit0->unbind(LLTexUnit::TT_TEXTURE);
  1204. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  1205. }
  1206. else
  1207. {
  1208. success = false;
  1209. }
  1210. }
  1211. else if (infop->mLocalTexture >= 0 &&
  1212. infop->mLocalTexture < TEX_NUM_INDICES)
  1213. {
  1214. LLGLTexture* texp = mLocalTextureObject->getImage();
  1215. if (texp)
  1216. {
  1217. gAlphaMaskProgram.setMinimumAlpha(0.f);
  1218. LLTexUnit* unit0 = gGL.getTexUnit(0);
  1219. unit0->bind(texp);
  1220. gl_rect_2d_simple_tex(width, height);
  1221. unit0->unbind(LLTexUnit::TT_TEXTURE);
  1222. success = true;
  1223. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  1224. }
  1225. }
  1226. return success;
  1227. }
  1228. //virtual
  1229. void LLTexLayer::gatherAlphaMasks(U8* data, S32 origin_x, S32 origin_y,
  1230. S32 width, S32 height)
  1231. {
  1232. addAlphaMask(data, origin_x, origin_y, width, height);
  1233. }
  1234. void LLTexLayer::renderMorphMasks(S32 x, S32 y, S32 width, S32 height,
  1235. const LLColor4& layer_color,
  1236. bool force_render)
  1237. {
  1238. LL_TRACY_TIMER(TRC_RENDER_MORPH_MASKS);
  1239. if (!force_render && !hasMorph())
  1240. {
  1241. LL_DEBUGS("TexLayer") << "Skipping renderMorphMasks for " << getUUID()
  1242. << LL_ENDL;
  1243. return;
  1244. }
  1245. bool success = true;
  1246. llassert(!mParamAlphaList.empty());
  1247. gAlphaMaskProgram.setMinimumAlpha(0.f);
  1248. gGL.setColorMask(false, true);
  1249. LLTexLayerParamAlpha* first_paramp = *mParamAlphaList.begin();
  1250. // Note: if the first param is a mulitply, multiply against the current
  1251. // buffer's alpha
  1252. if (!first_paramp || !first_paramp->getMultiplyBlend())
  1253. {
  1254. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1255. // Clear the alpha
  1256. gGL.setSceneBlendType(LLRender::BT_REPLACE);
  1257. gGL.color4f(0.f, 0.f, 0.f, 0.f);
  1258. gl_rect_2d_simple(width, height);
  1259. }
  1260. // Accumulate alphas
  1261. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  1262. for (param_alpha_list_t::iterator iter = mParamAlphaList.begin(),
  1263. end = mParamAlphaList.end();
  1264. iter != end; ++iter)
  1265. {
  1266. LLTexLayerParamAlpha* paramp = *iter;
  1267. success &= paramp->render(x, y, width, height);
  1268. if (!success && !force_render)
  1269. {
  1270. LL_DEBUGS("TexLayer") << "Failed to render param "
  1271. << paramp->getID()
  1272. << ", skipping morph mask." << LL_ENDL;
  1273. return;
  1274. }
  1275. }
  1276. // Approximates a min() function
  1277. gGL.setSceneBlendType(LLRender::BT_MULT_ALPHA);
  1278. const LLTexLayerInfo* infop = getInfo();
  1279. // Accumulate the alpha component of the texture
  1280. if (infop->mLocalTexture != -1)
  1281. {
  1282. LLGLTexture* texp = mLocalTextureObject->getImage();
  1283. if (texp && texp->getComponents() == 4)
  1284. {
  1285. LLTexUnit::eTextureAddressMode old_mode = texp->getAddressMode();
  1286. LLTexUnit* unit0 = gGL.getTexUnit(0);
  1287. unit0->bind(texp);
  1288. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  1289. gl_rect_2d_simple_tex(width, height);
  1290. unit0->setTextureAddressMode(old_mode);
  1291. unit0->unbind(LLTexUnit::TT_TEXTURE);
  1292. }
  1293. }
  1294. if (!infop->mStaticImageFileName.empty() && infop->mStaticImageIsMask)
  1295. {
  1296. LLGLTexture* texp =
  1297. gTexLayerStaticImageList.getTexture(infop->mStaticImageFileName,
  1298. infop->mStaticImageIsMask);
  1299. if (texp)
  1300. {
  1301. if (texp->getComponents() == 4 || texp->getComponents() == 1)
  1302. {
  1303. LLTexUnit* unit0 = gGL.getTexUnit(0);
  1304. unit0->bind(texp);
  1305. gl_rect_2d_simple_tex(width, height);
  1306. unit0->unbind(LLTexUnit::TT_TEXTURE);
  1307. }
  1308. else
  1309. {
  1310. llwarns << "Expected 1 or 4 components. Skipping rendering of "
  1311. << infop->mStaticImageFileName << " that got "
  1312. << texp->getComponents() << " components." << llendl;
  1313. }
  1314. }
  1315. }
  1316. // Draw a rectangle with the layer color to multiply the alpha by that
  1317. // color's alpha.
  1318. // Note: we're still using gGL.blendFunc(GL_DST_ALPHA, GL_ZERO);
  1319. if (!is_approx_equal(layer_color.mV[VW], 1.f))
  1320. {
  1321. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1322. gGL.color4fv(layer_color.mV);
  1323. gl_rect_2d_simple(width, height);
  1324. }
  1325. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  1326. LLGLSUIDefault gls_ui;
  1327. gGL.setColorMask(true, true);
  1328. if (hasMorph() && success)
  1329. {
  1330. LLCRC alpha_mask_crc;
  1331. const LLUUID& id = getUUID();
  1332. alpha_mask_crc.update((U8*)(&id.mData), UUID_BYTES);
  1333. for (param_alpha_list_t::const_iterator iter = mParamAlphaList.begin(),
  1334. end = mParamAlphaList.end();
  1335. iter != end; ++iter)
  1336. {
  1337. const LLTexLayerParamAlpha* param = *iter;
  1338. F32 param_weight = param->getWeight();
  1339. alpha_mask_crc.update((U8*)&param_weight, sizeof(F32));
  1340. }
  1341. U32 cache_index = alpha_mask_crc.getCRC();
  1342. U8* alpha_data = get_ptr_in_map(mAlphaCache, cache_index);
  1343. if (!alpha_data)
  1344. {
  1345. // Clear out a slot if we have filled our cache
  1346. S32 max_cache_entries =
  1347. getTexLayerSet()->getAvatarAppearance()->isSelf() ? 4 : 1;
  1348. while ((S32)mAlphaCache.size() >= max_cache_entries)
  1349. {
  1350. // Arbitrarily grab the first entry:
  1351. alpha_cache_t::iterator iter2 = mAlphaCache.begin();
  1352. alpha_data = iter2->second;
  1353. delete [] alpha_data;
  1354. mAlphaCache.hmap_erase(iter2);
  1355. }
  1356. alpha_data = new U8[width * height];
  1357. #if 0 // Old code. New code (introduced in PBR viewer) is below. HB
  1358. if (gGLManager.mIsIntel) // Work-around for broken Intel drivers
  1359. {
  1360. // Let's use a static buffer for speed. HB
  1361. static std::vector<U8> buffer;
  1362. buffer.reserve(width * height * 4);
  1363. glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
  1364. (void*)buffer.data());
  1365. for (U32 i = 0, count = width * height; i < count; ++i)
  1366. {
  1367. alpha_data[i] = buffer[i * 4 + 3];
  1368. }
  1369. }
  1370. else // Working drivers
  1371. {
  1372. glReadPixels(x, y, width, height, GL_ALPHA, GL_UNSIGNED_BYTE,
  1373. alpha_data);
  1374. }
  1375. #else // We just want GL_ALPHA, but it is not supported in GL core
  1376. // profile v4.
  1377. // Let's use a static buffer for speed. HB
  1378. static std::vector<U8> buffer;
  1379. buffer.reserve(width * height * 4);
  1380. glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
  1381. (void*)buffer.data());
  1382. for (U32 i = 0, count = width * height; i < count; ++i)
  1383. {
  1384. alpha_data[i] = buffer[i * 4 + 3];
  1385. }
  1386. #endif
  1387. stop_glerror();
  1388. mAlphaCache[cache_index] = alpha_data;
  1389. }
  1390. getTexLayerSet()->getAvatarAppearance()->dirtyMesh();
  1391. mMorphMasksValid = true;
  1392. getTexLayerSet()->applyMorphMask(alpha_data, width, height, 1);
  1393. }
  1394. }
  1395. void LLTexLayer::addAlphaMask(U8* datap, S32 origin_x, S32 origin_y, S32 width,
  1396. S32 height)
  1397. {
  1398. LL_TRACY_TIMER(TRC_ADD_ALPHA_MASK);
  1399. S32 size = width * height;
  1400. const U8* alpha_datap = getAlphaData();
  1401. if (!alpha_datap && hasAlphaParams())
  1402. {
  1403. LLColor4 net_color;
  1404. findNetColor(&net_color);
  1405. // *TODO: eliminate need for layer morph mask valid flag
  1406. invalidateMorphMasks();
  1407. renderMorphMasks(origin_x, origin_y, width, height, net_color, false);
  1408. alpha_datap = getAlphaData();
  1409. }
  1410. if (alpha_datap)
  1411. {
  1412. for (S32 i = 0; i < size; ++i)
  1413. {
  1414. U8 cur_alpha = datap[i];
  1415. U16 result_alpha = cur_alpha;
  1416. result_alpha *= (U16)alpha_datap[i] + 1;
  1417. result_alpha = result_alpha >> 8;
  1418. datap[i] = (U8)result_alpha;
  1419. }
  1420. }
  1421. }
  1422. //virtual
  1423. bool LLTexLayer::isInvisibleAlphaMask() const
  1424. {
  1425. return mLocalTextureObject &&
  1426. mLocalTextureObject->getID() == IMG_INVISIBLE;
  1427. }
  1428. LLUUID LLTexLayer::getUUID() const
  1429. {
  1430. LLUUID id;
  1431. const LLTexLayerInfo* infop = getInfo();
  1432. if (infop->mLocalTexture != -1)
  1433. {
  1434. LLGLTexture* tex = mLocalTextureObject->getImage();
  1435. if (tex)
  1436. {
  1437. id = mLocalTextureObject->getID();
  1438. }
  1439. }
  1440. if (!infop->mStaticImageFileName.empty())
  1441. {
  1442. LLGLTexture* texp =
  1443. gTexLayerStaticImageList.getTexture(infop->mStaticImageFileName,
  1444. infop->mStaticImageIsMask);
  1445. if (texp)
  1446. {
  1447. id = texp->getID();
  1448. }
  1449. }
  1450. return id;
  1451. }
  1452. //-----------------------------------------------------------------------------
  1453. // LLTexLayerTemplate class.
  1454. // A single texture layer, consisting of:
  1455. // * color, consisting of either
  1456. // * one or more color parameters (weighted colors)
  1457. // * a reference to a global color
  1458. // * a fixed color with non-zero alpha
  1459. // * opaque white (the default)
  1460. // * (optional) a texture defined by either
  1461. // * a GUID
  1462. // * a texture entry index (TE)
  1463. // * (optional) one or more alpha parameters (weighted alpha textures)
  1464. //-----------------------------------------------------------------------------
  1465. LLTexLayerTemplate::LLTexLayerTemplate(LLTexLayerSet* layer_setp,
  1466. LLAvatarAppearance* const appearance)
  1467. : LLTexLayerInterface(layer_setp),
  1468. mAvatarAppearance(appearance)
  1469. {
  1470. }
  1471. LLTexLayerTemplate::LLTexLayerTemplate(const LLTexLayerTemplate& layer)
  1472. : LLTexLayerInterface(layer),
  1473. mAvatarAppearance(layer.getAvatarAppearance())
  1474. {
  1475. }
  1476. //virtual
  1477. bool LLTexLayerTemplate::setInfo(const LLTexLayerInfo* info,
  1478. LLWearable* wearable)
  1479. {
  1480. return LLTexLayerInterface::setInfo(info, wearable);
  1481. }
  1482. U32 LLTexLayerTemplate::updateWearableCache() const
  1483. {
  1484. mWearableCache.clear();
  1485. LLWearableType::EType type = getWearableType();
  1486. if (type == LLWearableType::WT_INVALID)
  1487. {
  1488. // This layer cannot be cloned
  1489. return 0;
  1490. }
  1491. U32 num_wearables =
  1492. getAvatarAppearance()->getWearableData()->getWearableCount(type);
  1493. U32 added = 0;
  1494. for (U32 i = 0; i < num_wearables; ++i)
  1495. {
  1496. LLWearable* wearablep =
  1497. getAvatarAppearance()->getWearableData()->getWearable(type, i);
  1498. if (wearablep)
  1499. {
  1500. mWearableCache.push_back(wearablep);
  1501. ++added;
  1502. }
  1503. }
  1504. return added;
  1505. }
  1506. LLTexLayer* LLTexLayerTemplate::getLayer(U32 i) const
  1507. {
  1508. if (mWearableCache.size() <= i)
  1509. {
  1510. return NULL;
  1511. }
  1512. LLWearable* wearablep = mWearableCache[i];
  1513. LLLocalTextureObject* ltop = NULL;
  1514. LLTexLayer* layerp = NULL;
  1515. if (wearablep)
  1516. {
  1517. ltop = wearablep->getLocalTextureObject(mInfo->mLocalTexture);
  1518. }
  1519. if (ltop)
  1520. {
  1521. layerp = ltop->getTexLayer(getName());
  1522. }
  1523. return layerp;
  1524. }
  1525. //virtual
  1526. bool LLTexLayerTemplate::render(S32 x, S32 y, S32 width, S32 height)
  1527. {
  1528. if (!mInfo)
  1529. {
  1530. return false;
  1531. }
  1532. bool success = true;
  1533. updateWearableCache();
  1534. for (wearable_cache_t::const_iterator iter = mWearableCache.begin(),
  1535. end = mWearableCache.end();
  1536. iter!= end; ++iter)
  1537. {
  1538. LLWearable* wearablep = *iter;
  1539. LLLocalTextureObject* ltop = NULL;
  1540. LLTexLayer* layerp = NULL;
  1541. if (wearablep)
  1542. {
  1543. ltop = wearablep->getLocalTextureObject(mInfo->mLocalTexture);
  1544. }
  1545. if (ltop)
  1546. {
  1547. layerp = ltop->getTexLayer(getName());
  1548. }
  1549. if (layerp)
  1550. {
  1551. wearablep->writeToAvatar(mAvatarAppearance);
  1552. layerp->setLTO(ltop);
  1553. success &= layerp->render(x, y, width, height);
  1554. }
  1555. }
  1556. return success;
  1557. }
  1558. // Multiplies a single alpha texture against the frame buffer
  1559. //virtual
  1560. bool LLTexLayerTemplate::blendAlphaTexture(S32 x, S32 y, S32 width, S32 height)
  1561. {
  1562. bool success = true;
  1563. U32 num_wearables = updateWearableCache();
  1564. for (U32 i = 0; i < num_wearables; ++i)
  1565. {
  1566. LLTexLayer* layerp = getLayer(i);
  1567. if (layerp)
  1568. {
  1569. success &= layerp->blendAlphaTexture(x, y, width, height);
  1570. }
  1571. }
  1572. return success;
  1573. }
  1574. //virtual
  1575. void LLTexLayerTemplate::gatherAlphaMasks(U8* datap, S32 origin_x, S32 origin_y,
  1576. S32 width, S32 height)
  1577. {
  1578. U32 num_wearables = updateWearableCache();
  1579. for (U32 i = 0; i < num_wearables; ++i)
  1580. {
  1581. LLTexLayer* layerp = getLayer(i);
  1582. if (layerp)
  1583. {
  1584. layerp->addAlphaMask(datap, origin_x, origin_y, width, height);
  1585. }
  1586. }
  1587. }
  1588. //virtual
  1589. void LLTexLayerTemplate::setHasMorph(bool newval)
  1590. {
  1591. mHasMorph = newval;
  1592. U32 num_wearables = updateWearableCache();
  1593. for (U32 i = 0; i < num_wearables; ++i)
  1594. {
  1595. LLTexLayer* layerp = getLayer(i);
  1596. if (layerp)
  1597. {
  1598. layerp->setHasMorph(newval);
  1599. }
  1600. }
  1601. }
  1602. //virtual
  1603. void LLTexLayerTemplate::deleteCaches()
  1604. {
  1605. U32 num_wearables = updateWearableCache();
  1606. for (U32 i = 0; i < num_wearables; ++i)
  1607. {
  1608. LLTexLayer* layerp = getLayer(i);
  1609. if (layerp)
  1610. {
  1611. layerp->deleteCaches();
  1612. }
  1613. }
  1614. }
  1615. //virtual
  1616. bool LLTexLayerTemplate::isInvisibleAlphaMask() const
  1617. {
  1618. U32 num_wearables = updateWearableCache();
  1619. for (U32 i = 0; i < num_wearables; ++i)
  1620. {
  1621. LLTexLayer* layerp = getLayer(i);
  1622. if (layerp)
  1623. {
  1624. if (layerp->isInvisibleAlphaMask())
  1625. {
  1626. return true;
  1627. }
  1628. }
  1629. }
  1630. return false;
  1631. }
  1632. // Finds a specific layer based on a passed in name
  1633. LLTexLayerInterface* LLTexLayerSet::findLayerByName(const std::string& name)
  1634. {
  1635. for (layer_list_t::iterator iter = mLayerList.begin(),
  1636. end = mLayerList.end();
  1637. iter != end; ++iter)
  1638. {
  1639. LLTexLayerInterface* layerp = *iter;
  1640. if (layerp->getName() == name)
  1641. {
  1642. return layerp;
  1643. }
  1644. }
  1645. for (layer_list_t::iterator iter = mMaskLayerList.begin(),
  1646. end = mMaskLayerList.end();
  1647. iter != end; ++iter)
  1648. {
  1649. LLTexLayerInterface* layerp = *iter;
  1650. if (layerp->getName() == name)
  1651. {
  1652. return layerp;
  1653. }
  1654. }
  1655. return NULL;
  1656. }
  1657. void LLTexLayerSet::cloneTemplates(LLLocalTextureObject* lto,
  1658. LLAvatarAppearanceDefines::ETextureIndex tex_index,
  1659. LLWearable* wearable)
  1660. {
  1661. // Initialize all texlayers with this texture type for this LTO
  1662. for (LLTexLayerSet::layer_list_t::iterator iter = mLayerList.begin(),
  1663. end = mLayerList.end();
  1664. iter != end; ++iter)
  1665. {
  1666. LLTexLayerTemplate* layer = (LLTexLayerTemplate*)*iter;
  1667. if (layer->getInfo()->getLocalTexture() == (S32)tex_index)
  1668. {
  1669. lto->addTexLayer(layer, wearable);
  1670. }
  1671. }
  1672. for (LLTexLayerSet::layer_list_t::iterator iter = mMaskLayerList.begin(),
  1673. end = mMaskLayerList.end();
  1674. iter != end; ++iter)
  1675. {
  1676. LLTexLayerTemplate* layer = (LLTexLayerTemplate*)*iter;
  1677. if (layer->getInfo()->getLocalTexture() == (S32)tex_index)
  1678. {
  1679. lto->addTexLayer(layer, wearable);
  1680. }
  1681. }
  1682. }
  1683. //-----------------------------------------------------------------------------
  1684. // LLTexLayerStaticImageList class
  1685. //-----------------------------------------------------------------------------
  1686. LLTexLayerStaticImageList::LLTexLayerStaticImageList()
  1687. : mGLBytes(0),
  1688. mTGABytes(0),
  1689. mImageNames(16384)
  1690. {
  1691. }
  1692. LLTexLayerStaticImageList::~LLTexLayerStaticImageList()
  1693. {
  1694. deleteCachedImages();
  1695. }
  1696. void LLTexLayerStaticImageList::dumpByteCount() const
  1697. {
  1698. llinfos << "Avatar static textures " << "KB GL:" << mGLBytes / 1024
  1699. << "KB TGA:" << mTGABytes / 1024 << "KB" << llendl;
  1700. }
  1701. void LLTexLayerStaticImageList::deleteCachedImages()
  1702. {
  1703. if (mGLBytes || mTGABytes)
  1704. {
  1705. llinfos << "Clearing static textures " << "KB GL:" << mGLBytes / 1024
  1706. << "KB TGA:" << mTGABytes / 1024 << "KB" << llendl;
  1707. mStaticImageListTGA.clear();
  1708. // mStaticImageLists uses LLPointers, clear() will cause deletion
  1709. mStaticImageList.clear();
  1710. mGLBytes = 0;
  1711. mTGABytes = 0;
  1712. }
  1713. }
  1714. // Note: in general, for a given image image we'll call either getImageTga() or
  1715. // getTexture(). We call getImageTga() if the image is used as an alpha
  1716. //gradient. Otherwise, we call getTexture()
  1717. // Returns an LLImageTGA that contains the encoded data from a tga file named
  1718. // file_name. Caches the result to speed identical subsequent requests.
  1719. LLImageTGA* LLTexLayerStaticImageList::getImageTGA(const std::string& file_name)
  1720. {
  1721. LL_TRACY_TIMER(TRC_LOAD_STATIC_TGA);
  1722. const char* namekey = mImageNames.addString(file_name);
  1723. image_tga_map_t::const_iterator iter = mStaticImageListTGA.find(namekey);
  1724. if (iter != mStaticImageListTGA.end())
  1725. {
  1726. return iter->second;
  1727. }
  1728. std::string path = gDirUtil.getFullPath(LL_PATH_CHARACTER, file_name);
  1729. LLPointer<LLImageTGA> imagep = new LLImageTGA(path);
  1730. if (imagep->getDataSize() > 0)
  1731. {
  1732. mStaticImageListTGA[namekey] = imagep;
  1733. mTGABytes += imagep->getDataSize();
  1734. return imagep;
  1735. }
  1736. return NULL;
  1737. }
  1738. // Returns a GL Image (without a backing ImageRaw) that contains the decoded
  1739. // data from a tga file named file_name. Caches the result to speed identical
  1740. // subsequent requests.
  1741. LLGLTexture* LLTexLayerStaticImageList::getTexture(const std::string& file_name,
  1742. bool is_mask)
  1743. {
  1744. LL_TRACY_TIMER(TRC_LOAD_STATIC_TEXTURE);
  1745. LLPointer<LLGLTexture> tex = NULL;
  1746. const char* namekey = mImageNames.addString(file_name);
  1747. texture_map_t::const_iterator iter = mStaticImageList.find(namekey);
  1748. if (iter != mStaticImageList.end())
  1749. {
  1750. tex = iter->second;
  1751. }
  1752. else if (gTextureManagerBridgep)
  1753. {
  1754. tex = gTextureManagerBridgep->getLocalTexture(false);
  1755. LLPointer<LLImageRaw> image_raw = new LLImageRaw;
  1756. if (loadImageRaw(file_name, image_raw))
  1757. {
  1758. if (is_mask && image_raw->getComponents() == 1)
  1759. {
  1760. // Convert grayscale alpha masks from single channel into RGBA.
  1761. // Fill RGB with black to allow fixed function gl calls to
  1762. // match shader implementation.
  1763. LLPointer<LLImageRaw> alpha_image_raw = image_raw;
  1764. image_raw = new LLImageRaw(image_raw->getWidth(),
  1765. image_raw->getHeight(), 4);
  1766. image_raw->copyUnscaledAlphaMask(alpha_image_raw,
  1767. LLColor4U::black);
  1768. }
  1769. tex->createGLTexture(0, image_raw, 0, true);
  1770. gGL.getTexUnit(0)->bind(tex);
  1771. tex->setAddressMode(LLTexUnit::TAM_CLAMP);
  1772. mStaticImageList[namekey] = tex;
  1773. mGLBytes += (S32)tex->getWidth() * tex->getHeight() *
  1774. tex->getComponents();
  1775. }
  1776. }
  1777. return tex;
  1778. }
  1779. // Reads a .tga file, decodes it, and puts the decoded data in image_raw.
  1780. // Returns true if successful.
  1781. bool LLTexLayerStaticImageList::loadImageRaw(const std::string& file_name,
  1782. LLImageRaw* image_raw)
  1783. {
  1784. LL_TRACY_TIMER(TRC_LOAD_IMAGE_RAW);
  1785. bool success = false;
  1786. std::string path = gDirUtil.getFullPath(LL_PATH_CHARACTER, file_name);
  1787. LLPointer<LLImageTGA> image_tga = new LLImageTGA(path);
  1788. if (image_tga->getDataSize() > 0)
  1789. {
  1790. // Copy data from tga to raw.
  1791. success = image_tga->decode(image_raw);
  1792. }
  1793. return success;
  1794. }