/indra/newview/lltexlayerparams.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 568 lines · 426 code · 85 blank · 57 comment · 75 complexity · dc967244923e72da7e64620547707f61 MD5 · raw file

  1. /**
  2. * @file lltexlayerparams.cpp
  3. * @brief Texture layer parameters
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "lltexlayerparams.h"
  28. #include "llagentcamera.h"
  29. #include "llimagetga.h"
  30. #include "lltexlayer.h"
  31. #include "llvoavatarself.h"
  32. #include "llwearable.h"
  33. #include "llui.h"
  34. //-----------------------------------------------------------------------------
  35. // LLTexLayerParam
  36. //-----------------------------------------------------------------------------
  37. LLTexLayerParam::LLTexLayerParam(LLTexLayerInterface *layer) :
  38. mTexLayer(layer),
  39. mAvatar(NULL)
  40. {
  41. if (mTexLayer != NULL)
  42. {
  43. mAvatar = mTexLayer->getTexLayerSet()->getAvatar();
  44. }
  45. else
  46. {
  47. llerrs << "LLTexLayerParam constructor passed with NULL reference for layer!" << llendl;
  48. }
  49. }
  50. LLTexLayerParam::LLTexLayerParam(LLVOAvatar *avatar) :
  51. mTexLayer(NULL)
  52. {
  53. mAvatar = avatar;
  54. }
  55. BOOL LLTexLayerParam::setInfo(LLViewerVisualParamInfo *info, BOOL add_to_avatar )
  56. {
  57. LLViewerVisualParam::setInfo(info);
  58. if (add_to_avatar)
  59. {
  60. mAvatar->addVisualParam( this);
  61. }
  62. return TRUE;
  63. }
  64. //-----------------------------------------------------------------------------
  65. // LLTexLayerParamAlpha
  66. //-----------------------------------------------------------------------------
  67. // static
  68. LLTexLayerParamAlpha::param_alpha_ptr_list_t LLTexLayerParamAlpha::sInstances;
  69. // static
  70. void LLTexLayerParamAlpha::dumpCacheByteCount()
  71. {
  72. S32 gl_bytes = 0;
  73. getCacheByteCount( &gl_bytes);
  74. llinfos << "Processed Alpha Texture Cache GL:" << (gl_bytes/1024) << "KB" << llendl;
  75. }
  76. // static
  77. void LLTexLayerParamAlpha::getCacheByteCount(S32* gl_bytes)
  78. {
  79. *gl_bytes = 0;
  80. for (param_alpha_ptr_list_t::iterator iter = sInstances.begin();
  81. iter != sInstances.end(); iter++)
  82. {
  83. LLTexLayerParamAlpha* instance = *iter;
  84. LLViewerTexture* tex = instance->mCachedProcessedTexture;
  85. if (tex)
  86. {
  87. S32 bytes = (S32)tex->getWidth() * tex->getHeight() * tex->getComponents();
  88. if (tex->hasGLTexture())
  89. {
  90. *gl_bytes += bytes;
  91. }
  92. }
  93. }
  94. }
  95. LLTexLayerParamAlpha::LLTexLayerParamAlpha(LLTexLayerInterface* layer) :
  96. LLTexLayerParam(layer),
  97. mCachedProcessedTexture(NULL),
  98. mNeedsCreateTexture(FALSE),
  99. mStaticImageInvalid(FALSE),
  100. mAvgDistortionVec(1.f, 1.f, 1.f),
  101. mCachedEffectiveWeight(0.f)
  102. {
  103. sInstances.push_front(this);
  104. }
  105. LLTexLayerParamAlpha::LLTexLayerParamAlpha(LLVOAvatar* avatar) :
  106. LLTexLayerParam(avatar),
  107. mCachedProcessedTexture(NULL),
  108. mNeedsCreateTexture(FALSE),
  109. mStaticImageInvalid(FALSE),
  110. mAvgDistortionVec(1.f, 1.f, 1.f),
  111. mCachedEffectiveWeight(0.f)
  112. {
  113. sInstances.push_front(this);
  114. }
  115. LLTexLayerParamAlpha::~LLTexLayerParamAlpha()
  116. {
  117. deleteCaches();
  118. sInstances.remove(this);
  119. }
  120. /*virtual*/ LLViewerVisualParam* LLTexLayerParamAlpha::cloneParam(LLWearable* wearable) const
  121. {
  122. LLTexLayerParamAlpha *new_param = new LLTexLayerParamAlpha(mTexLayer);
  123. *new_param = *this;
  124. return new_param;
  125. }
  126. void LLTexLayerParamAlpha::deleteCaches()
  127. {
  128. mStaticImageTGA = NULL; // deletes image
  129. mCachedProcessedTexture = NULL;
  130. mStaticImageRaw = NULL;
  131. mNeedsCreateTexture = FALSE;
  132. }
  133. BOOL LLTexLayerParamAlpha::getMultiplyBlend() const
  134. {
  135. return ((LLTexLayerParamAlphaInfo *)getInfo())->mMultiplyBlend;
  136. }
  137. void LLTexLayerParamAlpha::setWeight(F32 weight, BOOL upload_bake)
  138. {
  139. if (mIsAnimating || mTexLayer == NULL)
  140. {
  141. return;
  142. }
  143. F32 min_weight = getMinWeight();
  144. F32 max_weight = getMaxWeight();
  145. F32 new_weight = llclamp(weight, min_weight, max_weight);
  146. U8 cur_u8 = F32_to_U8(mCurWeight, min_weight, max_weight);
  147. U8 new_u8 = F32_to_U8(new_weight, min_weight, max_weight);
  148. if (cur_u8 != new_u8)
  149. {
  150. mCurWeight = new_weight;
  151. if ((mAvatar->getSex() & getSex()) && (mAvatar->isSelf() && !mIsDummy)) // only trigger a baked texture update if we're changing a wearable's visual param.
  152. {
  153. if (isAgentAvatarValid() && !gAgentAvatarp->isUsingBakedTextures())
  154. {
  155. upload_bake = FALSE;
  156. }
  157. mAvatar->invalidateComposite(mTexLayer->getTexLayerSet(), upload_bake);
  158. mTexLayer->invalidateMorphMasks();
  159. }
  160. }
  161. }
  162. void LLTexLayerParamAlpha::setAnimationTarget(F32 target_value, BOOL upload_bake)
  163. {
  164. // do not animate dummy parameters
  165. if (mIsDummy)
  166. {
  167. setWeight(target_value, upload_bake);
  168. return;
  169. }
  170. mTargetWeight = target_value;
  171. setWeight(target_value, upload_bake);
  172. mIsAnimating = TRUE;
  173. if (mNext)
  174. {
  175. mNext->setAnimationTarget(target_value, upload_bake);
  176. }
  177. }
  178. void LLTexLayerParamAlpha::animate(F32 delta, BOOL upload_bake)
  179. {
  180. if (mNext)
  181. {
  182. mNext->animate(delta, upload_bake);
  183. }
  184. }
  185. BOOL LLTexLayerParamAlpha::getSkip() const
  186. {
  187. if (!mTexLayer)
  188. {
  189. return TRUE;
  190. }
  191. const LLVOAvatar *avatar = mTexLayer->getTexLayerSet()->getAvatar();
  192. if (((LLTexLayerParamAlphaInfo *)getInfo())->mSkipIfZeroWeight)
  193. {
  194. F32 effective_weight = (avatar->getSex() & getSex()) ? mCurWeight : getDefaultWeight();
  195. if (is_approx_zero(effective_weight))
  196. {
  197. return TRUE;
  198. }
  199. }
  200. LLWearableType::EType type = (LLWearableType::EType)getWearableType();
  201. if ((type != LLWearableType::WT_INVALID) && !avatar->isWearingWearableType(type))
  202. {
  203. return TRUE;
  204. }
  205. return FALSE;
  206. }
  207. BOOL LLTexLayerParamAlpha::render(S32 x, S32 y, S32 width, S32 height)
  208. {
  209. BOOL success = TRUE;
  210. if (!mTexLayer)
  211. {
  212. return success;
  213. }
  214. F32 effective_weight = (mTexLayer->getTexLayerSet()->getAvatar()->getSex() & getSex()) ? mCurWeight : getDefaultWeight();
  215. BOOL weight_changed = effective_weight != mCachedEffectiveWeight;
  216. if (getSkip())
  217. {
  218. return success;
  219. }
  220. LLTexLayerParamAlphaInfo *info = (LLTexLayerParamAlphaInfo *)getInfo();
  221. gGL.flush();
  222. if (info->mMultiplyBlend)
  223. {
  224. gGL.blendFunc(LLRender::BF_DEST_ALPHA, LLRender::BF_ZERO); // Multiplication: approximates a min() function
  225. }
  226. else
  227. {
  228. gGL.setSceneBlendType(LLRender::BT_ADD); // Addition: approximates a max() function
  229. }
  230. if (!info->mStaticImageFileName.empty() && !mStaticImageInvalid)
  231. {
  232. if (mStaticImageTGA.isNull())
  233. {
  234. // Don't load the image file until we actually need it the first time. Like now.
  235. mStaticImageTGA = LLTexLayerStaticImageList::getInstance()->getImageTGA(info->mStaticImageFileName);
  236. // We now have something in one of our caches
  237. LLTexLayerSet::sHasCaches |= mStaticImageTGA.notNull() ? TRUE : FALSE;
  238. if (mStaticImageTGA.isNull())
  239. {
  240. llwarns << "Unable to load static file: " << info->mStaticImageFileName << llendl;
  241. mStaticImageInvalid = TRUE; // don't try again.
  242. return FALSE;
  243. }
  244. }
  245. const S32 image_tga_width = mStaticImageTGA->getWidth();
  246. const S32 image_tga_height = mStaticImageTGA->getHeight();
  247. if (!mCachedProcessedTexture ||
  248. (mCachedProcessedTexture->getWidth() != image_tga_width) ||
  249. (mCachedProcessedTexture->getHeight() != image_tga_height) ||
  250. (weight_changed))
  251. {
  252. // llinfos << "Building Cached Alpha: " << mName << ": (" << mStaticImageRaw->getWidth() << ", " << mStaticImageRaw->getHeight() << ") " << effective_weight << llendl;
  253. mCachedEffectiveWeight = effective_weight;
  254. if (!mCachedProcessedTexture)
  255. {
  256. mCachedProcessedTexture = LLViewerTextureManager::getLocalTexture(image_tga_width, image_tga_height, 1, FALSE);
  257. // We now have something in one of our caches
  258. LLTexLayerSet::sHasCaches |= mCachedProcessedTexture ? TRUE : FALSE;
  259. mCachedProcessedTexture->setExplicitFormat(GL_ALPHA8, GL_ALPHA);
  260. }
  261. // Applies domain and effective weight to data as it is decoded. Also resizes the raw image if needed.
  262. mStaticImageRaw = NULL;
  263. mStaticImageRaw = new LLImageRaw;
  264. mStaticImageTGA->decodeAndProcess(mStaticImageRaw, info->mDomain, effective_weight);
  265. mNeedsCreateTexture = TRUE;
  266. }
  267. if (mCachedProcessedTexture)
  268. {
  269. {
  270. // Create the GL texture, and then hang onto it for future use.
  271. if (mNeedsCreateTexture)
  272. {
  273. mCachedProcessedTexture->createGLTexture(0, mStaticImageRaw);
  274. mNeedsCreateTexture = FALSE;
  275. gGL.getTexUnit(0)->bind(mCachedProcessedTexture);
  276. mCachedProcessedTexture->setAddressMode(LLTexUnit::TAM_CLAMP);
  277. }
  278. LLGLSNoAlphaTest gls_no_alpha_test;
  279. gGL.getTexUnit(0)->bind(mCachedProcessedTexture);
  280. gl_rect_2d_simple_tex(width, height);
  281. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  282. stop_glerror();
  283. }
  284. }
  285. // Don't keep the cache for other people's avatars
  286. // (It's not really a "cache" in that case, but the logic is the same)
  287. if (!mAvatar->isSelf())
  288. {
  289. mCachedProcessedTexture = NULL;
  290. }
  291. }
  292. else
  293. {
  294. LLGLDisable no_alpha(GL_ALPHA_TEST);
  295. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  296. gGL.color4f(0.f, 0.f, 0.f, effective_weight);
  297. gl_rect_2d_simple(width, height);
  298. }
  299. return success;
  300. }
  301. //-----------------------------------------------------------------------------
  302. // LLTexLayerParamAlphaInfo
  303. //-----------------------------------------------------------------------------
  304. LLTexLayerParamAlphaInfo::LLTexLayerParamAlphaInfo() :
  305. mMultiplyBlend(FALSE),
  306. mSkipIfZeroWeight(FALSE),
  307. mDomain(0.f)
  308. {
  309. }
  310. BOOL LLTexLayerParamAlphaInfo::parseXml(LLXmlTreeNode* node)
  311. {
  312. llassert(node->hasName("param") && node->getChildByName("param_alpha"));
  313. if (!LLViewerVisualParamInfo::parseXml(node))
  314. return FALSE;
  315. LLXmlTreeNode* param_alpha_node = node->getChildByName("param_alpha");
  316. if (!param_alpha_node)
  317. {
  318. return FALSE;
  319. }
  320. static LLStdStringHandle tga_file_string = LLXmlTree::addAttributeString("tga_file");
  321. if (param_alpha_node->getFastAttributeString(tga_file_string, mStaticImageFileName))
  322. {
  323. // Don't load the image file until it's actually needed.
  324. }
  325. // else
  326. // {
  327. // llwarns << "<param_alpha> element is missing tga_file attribute." << llendl;
  328. // }
  329. static LLStdStringHandle multiply_blend_string = LLXmlTree::addAttributeString("multiply_blend");
  330. param_alpha_node->getFastAttributeBOOL(multiply_blend_string, mMultiplyBlend);
  331. static LLStdStringHandle skip_if_zero_string = LLXmlTree::addAttributeString("skip_if_zero");
  332. param_alpha_node->getFastAttributeBOOL(skip_if_zero_string, mSkipIfZeroWeight);
  333. static LLStdStringHandle domain_string = LLXmlTree::addAttributeString("domain");
  334. param_alpha_node->getFastAttributeF32(domain_string, mDomain);
  335. return TRUE;
  336. }
  337. LLTexLayerParamColor::LLTexLayerParamColor(LLTexLayerInterface* layer) :
  338. LLTexLayerParam(layer),
  339. mAvgDistortionVec(1.f, 1.f, 1.f)
  340. {
  341. }
  342. LLTexLayerParamColor::LLTexLayerParamColor(LLVOAvatar *avatar) :
  343. LLTexLayerParam(avatar),
  344. mAvgDistortionVec(1.f, 1.f, 1.f)
  345. {
  346. }
  347. LLTexLayerParamColor::~LLTexLayerParamColor()
  348. {
  349. }
  350. /*virtual*/ LLViewerVisualParam* LLTexLayerParamColor::cloneParam(LLWearable* wearable) const
  351. {
  352. LLTexLayerParamColor *new_param = new LLTexLayerParamColor(mTexLayer);
  353. *new_param = *this;
  354. return new_param;
  355. }
  356. LLColor4 LLTexLayerParamColor::getNetColor() const
  357. {
  358. const LLTexLayerParamColorInfo *info = (LLTexLayerParamColorInfo *)getInfo();
  359. llassert(info->mNumColors >= 1);
  360. F32 effective_weight = (mAvatar && (mAvatar->getSex() & getSex())) ? mCurWeight : getDefaultWeight();
  361. S32 index_last = info->mNumColors - 1;
  362. F32 scaled_weight = effective_weight * index_last;
  363. S32 index_start = (S32) scaled_weight;
  364. S32 index_end = index_start + 1;
  365. if (index_start == index_last)
  366. {
  367. return info->mColors[index_last];
  368. }
  369. else
  370. {
  371. F32 weight = scaled_weight - index_start;
  372. const LLColor4 *start = &info->mColors[ index_start ];
  373. const LLColor4 *end = &info->mColors[ index_end ];
  374. return LLColor4((1.f - weight) * start->mV[VX] + weight * end->mV[VX],
  375. (1.f - weight) * start->mV[VY] + weight * end->mV[VY],
  376. (1.f - weight) * start->mV[VZ] + weight * end->mV[VZ],
  377. (1.f - weight) * start->mV[VW] + weight * end->mV[VW]);
  378. }
  379. }
  380. void LLTexLayerParamColor::setWeight(F32 weight, BOOL upload_bake)
  381. {
  382. if (mIsAnimating)
  383. {
  384. return;
  385. }
  386. const LLTexLayerParamColorInfo *info = (LLTexLayerParamColorInfo *)getInfo();
  387. F32 min_weight = getMinWeight();
  388. F32 max_weight = getMaxWeight();
  389. F32 new_weight = llclamp(weight, min_weight, max_weight);
  390. U8 cur_u8 = F32_to_U8(mCurWeight, min_weight, max_weight);
  391. U8 new_u8 = F32_to_U8(new_weight, min_weight, max_weight);
  392. if (cur_u8 != new_u8)
  393. {
  394. mCurWeight = new_weight;
  395. if (info->mNumColors <= 0)
  396. {
  397. // This will happen when we set the default weight the first time.
  398. return;
  399. }
  400. if ((mAvatar->getSex() & getSex()) && (mAvatar->isSelf() && !mIsDummy)) // only trigger a baked texture update if we're changing a wearable's visual param.
  401. {
  402. onGlobalColorChanged(upload_bake);
  403. if (mTexLayer)
  404. {
  405. mAvatar->invalidateComposite(mTexLayer->getTexLayerSet(), upload_bake);
  406. }
  407. }
  408. // llinfos << "param " << mName << " = " << new_weight << llendl;
  409. }
  410. }
  411. void LLTexLayerParamColor::setAnimationTarget(F32 target_value, BOOL upload_bake)
  412. {
  413. // set value first then set interpolating flag to ignore further updates
  414. mTargetWeight = target_value;
  415. setWeight(target_value, upload_bake);
  416. mIsAnimating = TRUE;
  417. if (mNext)
  418. {
  419. mNext->setAnimationTarget(target_value, upload_bake);
  420. }
  421. }
  422. void LLTexLayerParamColor::animate(F32 delta, BOOL upload_bake)
  423. {
  424. if (mNext)
  425. {
  426. mNext->animate(delta, upload_bake);
  427. }
  428. }
  429. //-----------------------------------------------------------------------------
  430. // LLTexLayerParamColorInfo
  431. //-----------------------------------------------------------------------------
  432. LLTexLayerParamColorInfo::LLTexLayerParamColorInfo() :
  433. mOperation(LLTexLayerParamColor::OP_ADD),
  434. mNumColors(0)
  435. {
  436. }
  437. BOOL LLTexLayerParamColorInfo::parseXml(LLXmlTreeNode *node)
  438. {
  439. llassert(node->hasName("param") && node->getChildByName("param_color"));
  440. if (!LLViewerVisualParamInfo::parseXml(node))
  441. return FALSE;
  442. LLXmlTreeNode* param_color_node = node->getChildByName("param_color");
  443. if (!param_color_node)
  444. {
  445. return FALSE;
  446. }
  447. std::string op_string;
  448. static LLStdStringHandle operation_string = LLXmlTree::addAttributeString("operation");
  449. if (param_color_node->getFastAttributeString(operation_string, op_string))
  450. {
  451. LLStringUtil::toLower(op_string);
  452. if (op_string == "add") mOperation = LLTexLayerParamColor::OP_ADD;
  453. else if (op_string == "multiply") mOperation = LLTexLayerParamColor::OP_MULTIPLY;
  454. else if (op_string == "blend") mOperation = LLTexLayerParamColor::OP_BLEND;
  455. }
  456. mNumColors = 0;
  457. LLColor4U color4u;
  458. for (LLXmlTreeNode* child = param_color_node->getChildByName("value");
  459. child;
  460. child = param_color_node->getNextNamedChild())
  461. {
  462. if ((mNumColors < MAX_COLOR_VALUES))
  463. {
  464. static LLStdStringHandle color_string = LLXmlTree::addAttributeString("color");
  465. if (child->getFastAttributeColor4U(color_string, color4u))
  466. {
  467. mColors[ mNumColors ].setVec(color4u);
  468. mNumColors++;
  469. }
  470. }
  471. }
  472. if (!mNumColors)
  473. {
  474. llwarns << "<param_color> is missing <value> sub-elements" << llendl;
  475. return FALSE;
  476. }
  477. if ((mOperation == LLTexLayerParamColor::OP_BLEND) && (mNumColors != 1))
  478. {
  479. llwarns << "<param_color> with operation\"blend\" must have exactly one <value>" << llendl;
  480. return FALSE;
  481. }
  482. return TRUE;
  483. }