PageRenderTime 96ms CodeModel.GetById 2ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/lltexlayer.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 2347 lines | 1810 code | 294 blank | 243 comment | 301 complexity | 018c418ac44468a479fe2dc343bd42b7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lltexlayer.cpp
  3. * @brief A texture layer. Used for avatars.
  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 "lltexlayer.h"
  28. #include "llagent.h"
  29. #include "llimagej2c.h"
  30. #include "llimagetga.h"
  31. #include "llnotificationsutil.h"
  32. #include "llvfile.h"
  33. #include "llvfs.h"
  34. #include "llviewerstats.h"
  35. #include "llviewerregion.h"
  36. #include "llvoavatar.h"
  37. #include "llvoavatarself.h"
  38. #include "pipeline.h"
  39. #include "llassetuploadresponders.h"
  40. #include "lltexlayerparams.h"
  41. #include "llui.h"
  42. #include "llagentwearables.h"
  43. #include "llwearable.h"
  44. #include "llviewercontrol.h"
  45. #include "llviewershadermgr.h"
  46. #include "llviewervisualparam.h"
  47. //#include "../tools/imdebug/imdebug.h"
  48. using namespace LLVOAvatarDefines;
  49. static const S32 BAKE_UPLOAD_ATTEMPTS = 7;
  50. static const F32 BAKE_UPLOAD_RETRY_DELAY = 2.f; // actual delay grows by power of 2 each attempt
  51. class LLTexLayerInfo
  52. {
  53. friend class LLTexLayer;
  54. friend class LLTexLayerTemplate;
  55. friend class LLTexLayerInterface;
  56. public:
  57. LLTexLayerInfo();
  58. ~LLTexLayerInfo();
  59. BOOL parseXml(LLXmlTreeNode* node);
  60. BOOL createVisualParams(LLVOAvatar *avatar);
  61. BOOL isUserSettable() { return mLocalTexture != -1; }
  62. S32 getLocalTexture() const { return mLocalTexture; }
  63. BOOL getOnlyAlpha() const { return mUseLocalTextureAlphaOnly; }
  64. std::string getName() const { return mName; }
  65. private:
  66. std::string mName;
  67. BOOL mWriteAllChannels; // Don't use masking. Just write RGBA into buffer,
  68. LLTexLayerInterface::ERenderPass mRenderPass;
  69. std::string mGlobalColor;
  70. LLColor4 mFixedColor;
  71. S32 mLocalTexture;
  72. std::string mStaticImageFileName;
  73. BOOL mStaticImageIsMask;
  74. BOOL mUseLocalTextureAlphaOnly; // Ignore RGB channels from the input texture. Use alpha as a mask
  75. BOOL mIsVisibilityMask;
  76. typedef std::vector< std::pair< std::string,BOOL > > morph_name_list_t;
  77. morph_name_list_t mMorphNameList;
  78. param_color_info_list_t mParamColorInfoList;
  79. param_alpha_info_list_t mParamAlphaInfoList;
  80. };
  81. //-----------------------------------------------------------------------------
  82. // LLBakedUploadData()
  83. //-----------------------------------------------------------------------------
  84. LLBakedUploadData::LLBakedUploadData(const LLVOAvatarSelf* avatar,
  85. LLTexLayerSet* layerset,
  86. const LLUUID& id,
  87. bool highest_res) :
  88. mAvatar(avatar),
  89. mTexLayerSet(layerset),
  90. mID(id),
  91. mStartTime(LLFrameTimer::getTotalTime()), // Record starting time
  92. mIsHighestRes(highest_res)
  93. {
  94. }
  95. //-----------------------------------------------------------------------------
  96. // LLTexLayerSetBuffer
  97. // The composite image that a LLTexLayerSet writes to. Each LLTexLayerSet has one.
  98. //-----------------------------------------------------------------------------
  99. // static
  100. S32 LLTexLayerSetBuffer::sGLByteCount = 0;
  101. LLTexLayerSetBuffer::LLTexLayerSetBuffer(LLTexLayerSet* const owner,
  102. S32 width, S32 height) :
  103. // ORDER_LAST => must render these after the hints are created.
  104. LLViewerDynamicTexture( width, height, 4, LLViewerDynamicTexture::ORDER_LAST, TRUE ),
  105. mUploadPending(FALSE), // Not used for any logic here, just to sync sending of updates
  106. mNeedsUpload(FALSE),
  107. mNumLowresUploads(0),
  108. mUploadFailCount(0),
  109. mNeedsUpdate(TRUE),
  110. mNumLowresUpdates(0),
  111. mTexLayerSet(owner)
  112. {
  113. LLTexLayerSetBuffer::sGLByteCount += getSize();
  114. mNeedsUploadTimer.start();
  115. mNeedsUpdateTimer.start();
  116. }
  117. LLTexLayerSetBuffer::~LLTexLayerSetBuffer()
  118. {
  119. LLTexLayerSetBuffer::sGLByteCount -= getSize();
  120. destroyGLTexture();
  121. for( S32 order = 0; order < ORDER_COUNT; order++ )
  122. {
  123. LLViewerDynamicTexture::sInstances[order].erase(this); // will fail in all but one case.
  124. }
  125. }
  126. //virtual
  127. S8 LLTexLayerSetBuffer::getType() const
  128. {
  129. return LLViewerDynamicTexture::LL_TEX_LAYER_SET_BUFFER ;
  130. }
  131. //virtual
  132. void LLTexLayerSetBuffer::restoreGLTexture()
  133. {
  134. LLViewerDynamicTexture::restoreGLTexture() ;
  135. }
  136. //virtual
  137. void LLTexLayerSetBuffer::destroyGLTexture()
  138. {
  139. LLViewerDynamicTexture::destroyGLTexture() ;
  140. }
  141. // static
  142. void LLTexLayerSetBuffer::dumpTotalByteCount()
  143. {
  144. llinfos << "Composite System GL Buffers: " << (LLTexLayerSetBuffer::sGLByteCount/1024) << "KB" << llendl;
  145. }
  146. void LLTexLayerSetBuffer::requestUpdate()
  147. {
  148. restartUpdateTimer();
  149. mNeedsUpdate = TRUE;
  150. mNumLowresUpdates = 0;
  151. // If we're in the middle of uploading a baked texture, we don't care about it any more.
  152. // When it's downloaded, ignore it.
  153. mUploadID.setNull();
  154. }
  155. void LLTexLayerSetBuffer::requestUpload()
  156. {
  157. conditionalRestartUploadTimer();
  158. mNeedsUpload = TRUE;
  159. mNumLowresUploads = 0;
  160. mUploadPending = TRUE;
  161. }
  162. void LLTexLayerSetBuffer::conditionalRestartUploadTimer()
  163. {
  164. // If we requested a new upload but haven't even uploaded
  165. // a low res version of our last upload request, then
  166. // keep the timer ticking instead of resetting it.
  167. if (mNeedsUpload && (mNumLowresUploads == 0))
  168. {
  169. mNeedsUploadTimer.unpause();
  170. }
  171. else
  172. {
  173. mNeedsUploadTimer.reset();
  174. mNeedsUploadTimer.start();
  175. }
  176. }
  177. void LLTexLayerSetBuffer::restartUpdateTimer()
  178. {
  179. mNeedsUpdateTimer.reset();
  180. mNeedsUpdateTimer.start();
  181. }
  182. void LLTexLayerSetBuffer::cancelUpload()
  183. {
  184. mNeedsUpload = FALSE;
  185. mUploadPending = FALSE;
  186. mNeedsUploadTimer.pause();
  187. mUploadRetryTimer.reset();
  188. }
  189. void LLTexLayerSetBuffer::pushProjection() const
  190. {
  191. gGL.matrixMode(LLRender::MM_PROJECTION);
  192. gGL.pushMatrix();
  193. gGL.loadIdentity();
  194. gGL.ortho(0.0f, mFullWidth, 0.0f, mFullHeight, -1.0f, 1.0f);
  195. gGL.matrixMode(LLRender::MM_MODELVIEW);
  196. gGL.pushMatrix();
  197. gGL.loadIdentity();
  198. }
  199. void LLTexLayerSetBuffer::popProjection() const
  200. {
  201. gGL.matrixMode(LLRender::MM_PROJECTION);
  202. gGL.popMatrix();
  203. gGL.matrixMode(LLRender::MM_MODELVIEW);
  204. gGL.popMatrix();
  205. }
  206. BOOL LLTexLayerSetBuffer::needsRender()
  207. {
  208. llassert(mTexLayerSet->getAvatar() == gAgentAvatarp);
  209. if (!isAgentAvatarValid()) return FALSE;
  210. const BOOL upload_now = mNeedsUpload && isReadyToUpload();
  211. const BOOL update_now = mNeedsUpdate && isReadyToUpdate();
  212. // Don't render if we don't want to (or aren't ready to) upload or update.
  213. if (!(update_now || upload_now))
  214. {
  215. return FALSE;
  216. }
  217. // Don't render if we're animating our appearance.
  218. if (gAgentAvatarp->getIsAppearanceAnimating())
  219. {
  220. return FALSE;
  221. }
  222. // Don't render if we are trying to create a shirt texture but aren't wearing a skirt.
  223. if (gAgentAvatarp->getBakedTE(mTexLayerSet) == LLVOAvatarDefines::TEX_SKIRT_BAKED &&
  224. !gAgentAvatarp->isWearingWearableType(LLWearableType::WT_SKIRT))
  225. {
  226. cancelUpload();
  227. return FALSE;
  228. }
  229. // Render if we have at least minimal level of detail for each local texture.
  230. return mTexLayerSet->isLocalTextureDataAvailable();
  231. }
  232. void LLTexLayerSetBuffer::preRender(BOOL clear_depth)
  233. {
  234. // Set up an ortho projection
  235. pushProjection();
  236. // keep depth buffer, we don't need to clear it
  237. LLViewerDynamicTexture::preRender(FALSE);
  238. }
  239. void LLTexLayerSetBuffer::postRender(BOOL success)
  240. {
  241. popProjection();
  242. LLViewerDynamicTexture::postRender(success);
  243. }
  244. BOOL LLTexLayerSetBuffer::render()
  245. {
  246. // Default color mask for tex layer render
  247. gGL.setColorMask(true, true);
  248. // do we need to upload, and do we have sufficient data to create an uploadable composite?
  249. // TODO: When do we upload the texture if gAgent.mNumPendingQueries is non-zero?
  250. const BOOL upload_now = mNeedsUpload && isReadyToUpload();
  251. const BOOL update_now = mNeedsUpdate && isReadyToUpdate();
  252. BOOL success = TRUE;
  253. bool use_shaders = LLGLSLShader::sNoFixedFunction;
  254. if (use_shaders)
  255. {
  256. gAlphaMaskProgram.bind();
  257. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  258. }
  259. LLVertexBuffer::unbind();
  260. // Composite the color data
  261. LLGLSUIDefault gls_ui;
  262. success &= mTexLayerSet->render( mOrigin.mX, mOrigin.mY, mFullWidth, mFullHeight );
  263. gGL.flush();
  264. if(upload_now)
  265. {
  266. if (!success)
  267. {
  268. llinfos << "Failed attempt to bake " << mTexLayerSet->getBodyRegionName() << llendl;
  269. mUploadPending = FALSE;
  270. }
  271. else
  272. {
  273. if (mTexLayerSet->isVisible())
  274. {
  275. mTexLayerSet->getAvatar()->debugBakedTextureUpload(mTexLayerSet->getBakedTexIndex(), FALSE); // FALSE for start of upload, TRUE for finish.
  276. doUpload();
  277. }
  278. else
  279. {
  280. mUploadPending = FALSE;
  281. mNeedsUpload = FALSE;
  282. mNeedsUploadTimer.pause();
  283. mTexLayerSet->getAvatar()->setNewBakedTexture(mTexLayerSet->getBakedTexIndex(),IMG_INVISIBLE);
  284. }
  285. }
  286. }
  287. if (update_now)
  288. {
  289. doUpdate();
  290. }
  291. if (use_shaders)
  292. {
  293. gAlphaMaskProgram.unbind();
  294. }
  295. LLVertexBuffer::unbind();
  296. // reset GL state
  297. gGL.setColorMask(true, true);
  298. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  299. // we have valid texture data now
  300. mGLTexturep->setGLTextureCreated(true);
  301. return success;
  302. }
  303. BOOL LLTexLayerSetBuffer::isInitialized(void) const
  304. {
  305. return mGLTexturep.notNull() && mGLTexturep->isGLTextureCreated();
  306. }
  307. BOOL LLTexLayerSetBuffer::uploadPending() const
  308. {
  309. return mUploadPending;
  310. }
  311. BOOL LLTexLayerSetBuffer::uploadNeeded() const
  312. {
  313. return mNeedsUpload;
  314. }
  315. BOOL LLTexLayerSetBuffer::uploadInProgress() const
  316. {
  317. return !mUploadID.isNull();
  318. }
  319. BOOL LLTexLayerSetBuffer::isReadyToUpload() const
  320. {
  321. if (!gAgentQueryManager.hasNoPendingQueries()) return FALSE; // Can't upload if there are pending queries.
  322. if (isAgentAvatarValid() && !gAgentAvatarp->isUsingBakedTextures()) return FALSE; // Don't upload if avatar is using composites.
  323. BOOL ready = FALSE;
  324. if (mTexLayerSet->isLocalTextureDataFinal())
  325. {
  326. // If we requested an upload and have the final LOD ready, upload (or wait a while if this is a retry)
  327. if (mUploadFailCount == 0)
  328. {
  329. ready = TRUE;
  330. }
  331. else
  332. {
  333. ready = mUploadRetryTimer.getElapsedTimeF32() >= BAKE_UPLOAD_RETRY_DELAY * (1 << (mUploadFailCount - 1));
  334. }
  335. }
  336. else
  337. {
  338. // Upload if we've hit a timeout. Upload is a pretty expensive process so we need to make sure
  339. // we aren't doing uploads too frequently.
  340. const U32 texture_timeout = gSavedSettings.getU32("AvatarBakedTextureUploadTimeout");
  341. if (texture_timeout != 0)
  342. {
  343. // The timeout period increases exponentially between every lowres upload in order to prevent
  344. // spamming the server with frequent uploads.
  345. const U32 texture_timeout_threshold = texture_timeout*(1 << mNumLowresUploads);
  346. // If we hit our timeout and have textures available at even lower resolution, then upload.
  347. const BOOL is_upload_textures_timeout = mNeedsUploadTimer.getElapsedTimeF32() >= texture_timeout_threshold;
  348. const BOOL has_lower_lod = mTexLayerSet->isLocalTextureDataAvailable();
  349. ready = has_lower_lod && is_upload_textures_timeout;
  350. }
  351. }
  352. return ready;
  353. }
  354. BOOL LLTexLayerSetBuffer::isReadyToUpdate() const
  355. {
  356. // If we requested an update and have the final LOD ready, then update.
  357. if (mTexLayerSet->isLocalTextureDataFinal()) return TRUE;
  358. // If we haven't done an update yet, then just do one now regardless of state of textures.
  359. if (mNumLowresUpdates == 0) return TRUE;
  360. // Update if we've hit a timeout. Unlike for uploads, we can make this timeout fairly small
  361. // since render unnecessarily doesn't cost much.
  362. const U32 texture_timeout = gSavedSettings.getU32("AvatarBakedLocalTextureUpdateTimeout");
  363. if (texture_timeout != 0)
  364. {
  365. // If we hit our timeout and have textures available at even lower resolution, then update.
  366. const BOOL is_update_textures_timeout = mNeedsUpdateTimer.getElapsedTimeF32() >= texture_timeout;
  367. const BOOL has_lower_lod = mTexLayerSet->isLocalTextureDataAvailable();
  368. if (has_lower_lod && is_update_textures_timeout) return TRUE;
  369. }
  370. return FALSE;
  371. }
  372. BOOL LLTexLayerSetBuffer::requestUpdateImmediate()
  373. {
  374. mNeedsUpdate = TRUE;
  375. BOOL result = FALSE;
  376. if (needsRender())
  377. {
  378. preRender(FALSE);
  379. result = render();
  380. postRender(result);
  381. }
  382. return result;
  383. }
  384. // Create the baked texture, send it out to the server, then wait for it to come
  385. // back so we can switch to using it.
  386. void LLTexLayerSetBuffer::doUpload()
  387. {
  388. llinfos << "Uploading baked " << mTexLayerSet->getBodyRegionName() << llendl;
  389. LLViewerStats::getInstance()->incStat(LLViewerStats::ST_TEX_BAKES);
  390. // Don't need caches since we're baked now. (note: we won't *really* be baked
  391. // until this image is sent to the server and the Avatar Appearance message is received.)
  392. mTexLayerSet->deleteCaches();
  393. // Get the COLOR information from our texture
  394. U8* baked_color_data = new U8[ mFullWidth * mFullHeight * 4 ];
  395. glReadPixels(mOrigin.mX, mOrigin.mY, mFullWidth, mFullHeight, GL_RGBA, GL_UNSIGNED_BYTE, baked_color_data );
  396. stop_glerror();
  397. // Get the MASK information from our texture
  398. LLGLSUIDefault gls_ui;
  399. LLPointer<LLImageRaw> baked_mask_image = new LLImageRaw(mFullWidth, mFullHeight, 1 );
  400. U8* baked_mask_data = baked_mask_image->getData();
  401. mTexLayerSet->gatherMorphMaskAlpha(baked_mask_data, mFullWidth, mFullHeight);
  402. // Create the baked image from our color and mask information
  403. const S32 baked_image_components = 5; // red green blue [bump] clothing
  404. LLPointer<LLImageRaw> baked_image = new LLImageRaw( mFullWidth, mFullHeight, baked_image_components );
  405. U8* baked_image_data = baked_image->getData();
  406. S32 i = 0;
  407. for (S32 u=0; u < mFullWidth; u++)
  408. {
  409. for (S32 v=0; v < mFullHeight; v++)
  410. {
  411. baked_image_data[5*i + 0] = baked_color_data[4*i + 0];
  412. baked_image_data[5*i + 1] = baked_color_data[4*i + 1];
  413. baked_image_data[5*i + 2] = baked_color_data[4*i + 2];
  414. baked_image_data[5*i + 3] = baked_color_data[4*i + 3]; // alpha should be correct for eyelashes.
  415. baked_image_data[5*i + 4] = baked_mask_data[i];
  416. i++;
  417. }
  418. }
  419. LLPointer<LLImageJ2C> compressedImage = new LLImageJ2C;
  420. compressedImage->setRate(0.f);
  421. const char* comment_text = LINDEN_J2C_COMMENT_PREFIX "RGBHM"; // writes into baked_color_data. 5 channels (rgb, heightfield/alpha, mask)
  422. if (compressedImage->encode(baked_image, comment_text))
  423. {
  424. LLTransactionID tid;
  425. tid.generate();
  426. const LLAssetID asset_id = tid.makeAssetID(gAgent.getSecureSessionID());
  427. if (LLVFile::writeFile(compressedImage->getData(), compressedImage->getDataSize(),
  428. gVFS, asset_id, LLAssetType::AT_TEXTURE))
  429. {
  430. // Read back the file and validate.
  431. BOOL valid = FALSE;
  432. LLPointer<LLImageJ2C> integrity_test = new LLImageJ2C;
  433. S32 file_size = 0;
  434. U8* data = LLVFile::readFile(gVFS, asset_id, LLAssetType::AT_TEXTURE, &file_size);
  435. if (data)
  436. {
  437. valid = integrity_test->validate(data, file_size); // integrity_test will delete 'data'
  438. }
  439. else
  440. {
  441. integrity_test->setLastError("Unable to read entire file");
  442. }
  443. if (valid)
  444. {
  445. const bool highest_lod = mTexLayerSet->isLocalTextureDataFinal();
  446. // Baked_upload_data is owned by the responder and deleted after the request completes.
  447. LLBakedUploadData* baked_upload_data = new LLBakedUploadData(gAgentAvatarp,
  448. this->mTexLayerSet,
  449. asset_id,
  450. highest_lod);
  451. // upload ID is used to avoid overlaps, e.g. when the user rapidly makes two changes outside of Face Edit.
  452. mUploadID = asset_id;
  453. // Upload the image
  454. const std::string url = gAgent.getRegion()->getCapability("UploadBakedTexture");
  455. if(!url.empty()
  456. && !LLPipeline::sForceOldBakedUpload // toggle debug setting UploadBakedTexOld to change between the new caps method and old method
  457. && (mUploadFailCount < (BAKE_UPLOAD_ATTEMPTS - 1))) // Try last ditch attempt via asset store if cap upload is failing.
  458. {
  459. LLSD body = LLSD::emptyMap();
  460. // The responder will call LLTexLayerSetBuffer::onTextureUploadComplete()
  461. LLHTTPClient::post(url, body, new LLSendTexLayerResponder(body, mUploadID, LLAssetType::AT_TEXTURE, baked_upload_data));
  462. llinfos << "Baked texture upload via capability of " << mUploadID << " to " << url << llendl;
  463. }
  464. else
  465. {
  466. gAssetStorage->storeAssetData(tid,
  467. LLAssetType::AT_TEXTURE,
  468. LLTexLayerSetBuffer::onTextureUploadComplete,
  469. baked_upload_data,
  470. TRUE, // temp_file
  471. TRUE, // is_priority
  472. TRUE); // store_local
  473. llinfos << "Baked texture upload via Asset Store." << llendl;
  474. }
  475. if (highest_lod)
  476. {
  477. // Sending the final LOD for the baked texture. All done, pause
  478. // the upload timer so we know how long it took.
  479. mNeedsUpload = FALSE;
  480. mNeedsUploadTimer.pause();
  481. }
  482. else
  483. {
  484. // Sending a lower level LOD for the baked texture. Restart the upload timer.
  485. mNumLowresUploads++;
  486. mNeedsUploadTimer.unpause();
  487. mNeedsUploadTimer.reset();
  488. }
  489. // Print out notification that we uploaded this texture.
  490. if (gSavedSettings.getBOOL("DebugAvatarRezTime"))
  491. {
  492. const std::string lod_str = highest_lod ? "HighRes" : "LowRes";
  493. LLSD args;
  494. args["EXISTENCE"] = llformat("%d",(U32)mTexLayerSet->getAvatar()->debugGetExistenceTimeElapsedF32());
  495. args["TIME"] = llformat("%d",(U32)mNeedsUploadTimer.getElapsedTimeF32());
  496. args["BODYREGION"] = mTexLayerSet->getBodyRegionName();
  497. args["RESOLUTION"] = lod_str;
  498. LLNotificationsUtil::add("AvatarRezSelfBakedTextureUploadNotification",args);
  499. llinfos << "Uploading [ name: " << mTexLayerSet->getBodyRegionName() << " res:" << lod_str << " time:" << (U32)mNeedsUploadTimer.getElapsedTimeF32() << " ]" << llendl;
  500. }
  501. }
  502. else
  503. {
  504. // The read back and validate operation failed. Remove the uploaded file.
  505. mUploadPending = FALSE;
  506. LLVFile file(gVFS, asset_id, LLAssetType::AT_TEXTURE, LLVFile::WRITE);
  507. file.remove();
  508. llinfos << "Unable to create baked upload file (reason: corrupted)." << llendl;
  509. }
  510. }
  511. }
  512. else
  513. {
  514. // The VFS write file operation failed.
  515. mUploadPending = FALSE;
  516. llinfos << "Unable to create baked upload file (reason: failed to write file)" << llendl;
  517. }
  518. delete [] baked_color_data;
  519. }
  520. // Mostly bookkeeping; don't need to actually "do" anything since
  521. // render() will actually do the update.
  522. void LLTexLayerSetBuffer::doUpdate()
  523. {
  524. const BOOL highest_lod = mTexLayerSet->isLocalTextureDataFinal();
  525. if (highest_lod)
  526. {
  527. mNeedsUpdate = FALSE;
  528. }
  529. else
  530. {
  531. mNumLowresUpdates++;
  532. }
  533. restartUpdateTimer();
  534. // need to swtich to using this layerset if this is the first update
  535. // after getting the lowest LOD
  536. mTexLayerSet->getAvatar()->updateMeshTextures();
  537. // Print out notification that we uploaded this texture.
  538. if (gSavedSettings.getBOOL("DebugAvatarRezTime"))
  539. {
  540. const BOOL highest_lod = mTexLayerSet->isLocalTextureDataFinal();
  541. const std::string lod_str = highest_lod ? "HighRes" : "LowRes";
  542. LLSD args;
  543. args["EXISTENCE"] = llformat("%d",(U32)mTexLayerSet->getAvatar()->debugGetExistenceTimeElapsedF32());
  544. args["TIME"] = llformat("%d",(U32)mNeedsUpdateTimer.getElapsedTimeF32());
  545. args["BODYREGION"] = mTexLayerSet->getBodyRegionName();
  546. args["RESOLUTION"] = lod_str;
  547. LLNotificationsUtil::add("AvatarRezSelfBakedTextureUpdateNotification",args);
  548. llinfos << "Locally updating [ name: " << mTexLayerSet->getBodyRegionName() << " res:" << lod_str << " time:" << (U32)mNeedsUpdateTimer.getElapsedTimeF32() << " ]" << llendl;
  549. }
  550. }
  551. // static
  552. void LLTexLayerSetBuffer::onTextureUploadComplete(const LLUUID& uuid,
  553. void* userdata,
  554. S32 result,
  555. LLExtStat ext_status) // StoreAssetData callback (not fixed)
  556. {
  557. LLBakedUploadData* baked_upload_data = (LLBakedUploadData*)userdata;
  558. if (isAgentAvatarValid() &&
  559. !gAgentAvatarp->isDead() &&
  560. (baked_upload_data->mAvatar == gAgentAvatarp) && // Sanity check: only the user's avatar should be uploading textures.
  561. (baked_upload_data->mTexLayerSet->hasComposite()))
  562. {
  563. LLTexLayerSetBuffer* layerset_buffer = baked_upload_data->mTexLayerSet->getComposite();
  564. S32 failures = layerset_buffer->mUploadFailCount;
  565. layerset_buffer->mUploadFailCount = 0;
  566. if (layerset_buffer->mUploadID.isNull())
  567. {
  568. // The upload got canceled, we should be in the
  569. // process of baking a new texture so request an
  570. // upload with the new data
  571. // BAP: does this really belong in this callback, as
  572. // opposed to where the cancellation takes place?
  573. // suspect this does nothing.
  574. layerset_buffer->requestUpload();
  575. }
  576. else if (baked_upload_data->mID == layerset_buffer->mUploadID)
  577. {
  578. // This is the upload we're currently waiting for.
  579. layerset_buffer->mUploadID.setNull();
  580. const std::string name(baked_upload_data->mTexLayerSet->getBodyRegionName());
  581. const std::string resolution = baked_upload_data->mIsHighestRes ? " full res " : " low res ";
  582. if (result >= 0)
  583. {
  584. layerset_buffer->mUploadPending = FALSE; // Allows sending of AgentSetAppearance later
  585. LLVOAvatarDefines::ETextureIndex baked_te = gAgentAvatarp->getBakedTE(layerset_buffer->mTexLayerSet);
  586. // Update baked texture info with the new UUID
  587. U64 now = LLFrameTimer::getTotalTime(); // Record starting time
  588. llinfos << "Baked" << resolution << "texture upload for " << name << " took " << (S32)((now - baked_upload_data->mStartTime) / 1000) << " ms" << llendl;
  589. gAgentAvatarp->setNewBakedTexture(baked_te, uuid);
  590. }
  591. else
  592. {
  593. ++failures;
  594. S32 max_attempts = baked_upload_data->mIsHighestRes ? BAKE_UPLOAD_ATTEMPTS : 1; // only retry final bakes
  595. llwarns << "Baked" << resolution << "texture upload for " << name << " failed (attempt " << failures << "/" << max_attempts << ")" << llendl;
  596. if (failures < max_attempts)
  597. {
  598. layerset_buffer->mUploadFailCount = failures;
  599. layerset_buffer->mUploadRetryTimer.start();
  600. layerset_buffer->requestUpload();
  601. }
  602. }
  603. }
  604. else
  605. {
  606. llinfos << "Received baked texture out of date, ignored." << llendl;
  607. }
  608. gAgentAvatarp->dirtyMesh();
  609. }
  610. else
  611. {
  612. // Baked texture failed to upload (in which case since we
  613. // didn't set the new baked texture, it means that they'll try
  614. // and rebake it at some point in the future (after login?)),
  615. // or this response to upload is out of date, in which case a
  616. // current response should be on the way or already processed.
  617. llwarns << "Baked upload failed" << llendl;
  618. }
  619. delete baked_upload_data;
  620. }
  621. //-----------------------------------------------------------------------------
  622. // LLTexLayerSet
  623. // An ordered set of texture layers that get composited into a single texture.
  624. //-----------------------------------------------------------------------------
  625. LLTexLayerSetInfo::LLTexLayerSetInfo() :
  626. mBodyRegion( "" ),
  627. mWidth( 512 ),
  628. mHeight( 512 ),
  629. mClearAlpha( TRUE )
  630. {
  631. }
  632. LLTexLayerSetInfo::~LLTexLayerSetInfo( )
  633. {
  634. std::for_each(mLayerInfoList.begin(), mLayerInfoList.end(), DeletePointer());
  635. }
  636. BOOL LLTexLayerSetInfo::parseXml(LLXmlTreeNode* node)
  637. {
  638. llassert( node->hasName( "layer_set" ) );
  639. if( !node->hasName( "layer_set" ) )
  640. {
  641. return FALSE;
  642. }
  643. // body_region
  644. static LLStdStringHandle body_region_string = LLXmlTree::addAttributeString("body_region");
  645. if( !node->getFastAttributeString( body_region_string, mBodyRegion ) )
  646. {
  647. llwarns << "<layer_set> is missing body_region attribute" << llendl;
  648. return FALSE;
  649. }
  650. // width, height
  651. static LLStdStringHandle width_string = LLXmlTree::addAttributeString("width");
  652. if( !node->getFastAttributeS32( width_string, mWidth ) )
  653. {
  654. return FALSE;
  655. }
  656. static LLStdStringHandle height_string = LLXmlTree::addAttributeString("height");
  657. if( !node->getFastAttributeS32( height_string, mHeight ) )
  658. {
  659. return FALSE;
  660. }
  661. // Optional alpha component to apply after all compositing is complete.
  662. static LLStdStringHandle alpha_tga_file_string = LLXmlTree::addAttributeString("alpha_tga_file");
  663. node->getFastAttributeString( alpha_tga_file_string, mStaticAlphaFileName );
  664. static LLStdStringHandle clear_alpha_string = LLXmlTree::addAttributeString("clear_alpha");
  665. node->getFastAttributeBOOL( clear_alpha_string, mClearAlpha );
  666. // <layer>
  667. for (LLXmlTreeNode* child = node->getChildByName( "layer" );
  668. child;
  669. child = node->getNextNamedChild())
  670. {
  671. LLTexLayerInfo* info = new LLTexLayerInfo();
  672. if( !info->parseXml( child ))
  673. {
  674. delete info;
  675. return FALSE;
  676. }
  677. mLayerInfoList.push_back( info );
  678. }
  679. return TRUE;
  680. }
  681. // creates visual params without generating layersets or layers
  682. void LLTexLayerSetInfo::createVisualParams(LLVOAvatar *avatar)
  683. {
  684. //layer_info_list_t mLayerInfoList;
  685. for (layer_info_list_t::iterator layer_iter = mLayerInfoList.begin();
  686. layer_iter != mLayerInfoList.end();
  687. layer_iter++)
  688. {
  689. LLTexLayerInfo *layer_info = *layer_iter;
  690. layer_info->createVisualParams(avatar);
  691. }
  692. }
  693. //-----------------------------------------------------------------------------
  694. // LLTexLayerSet
  695. // An ordered set of texture layers that get composited into a single texture.
  696. //-----------------------------------------------------------------------------
  697. BOOL LLTexLayerSet::sHasCaches = FALSE;
  698. LLTexLayerSet::LLTexLayerSet(LLVOAvatarSelf* const avatar) :
  699. mComposite( NULL ),
  700. mAvatar( avatar ),
  701. mUpdatesEnabled( FALSE ),
  702. mIsVisible( TRUE ),
  703. mBakedTexIndex(LLVOAvatarDefines::BAKED_HEAD),
  704. mInfo( NULL )
  705. {
  706. }
  707. LLTexLayerSet::~LLTexLayerSet()
  708. {
  709. deleteCaches();
  710. std::for_each(mLayerList.begin(), mLayerList.end(), DeletePointer());
  711. std::for_each(mMaskLayerList.begin(), mMaskLayerList.end(), DeletePointer());
  712. }
  713. //-----------------------------------------------------------------------------
  714. // setInfo
  715. //-----------------------------------------------------------------------------
  716. BOOL LLTexLayerSet::setInfo(const LLTexLayerSetInfo *info)
  717. {
  718. llassert(mInfo == NULL);
  719. mInfo = info;
  720. //mID = info->mID; // No ID
  721. mLayerList.reserve(info->mLayerInfoList.size());
  722. for (LLTexLayerSetInfo::layer_info_list_t::const_iterator iter = info->mLayerInfoList.begin();
  723. iter != info->mLayerInfoList.end();
  724. iter++)
  725. {
  726. LLTexLayerInterface *layer = NULL;
  727. if ( (*iter)->isUserSettable() )
  728. {
  729. layer = new LLTexLayerTemplate( this );
  730. }
  731. else
  732. {
  733. layer = new LLTexLayer(this);
  734. }
  735. // this is the first time this layer (of either type) is being created - make sure you add the parameters to the avatar
  736. if (!layer->setInfo(*iter, NULL))
  737. {
  738. mInfo = NULL;
  739. return FALSE;
  740. }
  741. if (!layer->isVisibilityMask())
  742. {
  743. mLayerList.push_back( layer );
  744. }
  745. else
  746. {
  747. mMaskLayerList.push_back(layer);
  748. }
  749. }
  750. requestUpdate();
  751. stop_glerror();
  752. return TRUE;
  753. }
  754. #if 0 // obsolete
  755. //-----------------------------------------------------------------------------
  756. // parseData
  757. //-----------------------------------------------------------------------------
  758. BOOL LLTexLayerSet::parseData(LLXmlTreeNode* node)
  759. {
  760. LLTexLayerSetInfo *info = new LLTexLayerSetInfo;
  761. if (!info->parseXml(node))
  762. {
  763. delete info;
  764. return FALSE;
  765. }
  766. if (!setInfo(info))
  767. {
  768. delete info;
  769. return FALSE;
  770. }
  771. return TRUE;
  772. }
  773. #endif
  774. void LLTexLayerSet::deleteCaches()
  775. {
  776. for( layer_list_t::iterator iter = mLayerList.begin(); iter != mLayerList.end(); iter++ )
  777. {
  778. LLTexLayerInterface* layer = *iter;
  779. layer->deleteCaches();
  780. }
  781. for (layer_list_t::iterator iter = mMaskLayerList.begin(); iter != mMaskLayerList.end(); iter++)
  782. {
  783. LLTexLayerInterface* layer = *iter;
  784. layer->deleteCaches();
  785. }
  786. }
  787. // Returns TRUE if at least one packet of data has been received for each of the textures that this layerset depends on.
  788. BOOL LLTexLayerSet::isLocalTextureDataAvailable() const
  789. {
  790. if (!mAvatar->isSelf()) return FALSE;
  791. return ((LLVOAvatarSelf *)mAvatar)->isLocalTextureDataAvailable(this);
  792. }
  793. // Returns TRUE if all of the data for the textures that this layerset depends on have arrived.
  794. BOOL LLTexLayerSet::isLocalTextureDataFinal() const
  795. {
  796. if (!mAvatar->isSelf()) return FALSE;
  797. return ((LLVOAvatarSelf *)mAvatar)->isLocalTextureDataFinal(this);
  798. }
  799. BOOL LLTexLayerSet::render( S32 x, S32 y, S32 width, S32 height )
  800. {
  801. BOOL success = TRUE;
  802. mIsVisible = TRUE;
  803. if (mMaskLayerList.size() > 0)
  804. {
  805. for (layer_list_t::iterator iter = mMaskLayerList.begin(); iter != mMaskLayerList.end(); iter++)
  806. {
  807. LLTexLayerInterface* layer = *iter;
  808. if (layer->isInvisibleAlphaMask())
  809. {
  810. mIsVisible = FALSE;
  811. }
  812. }
  813. }
  814. bool use_shaders = LLGLSLShader::sNoFixedFunction;
  815. LLGLSUIDefault gls_ui;
  816. LLGLDepthTest gls_depth(GL_FALSE, GL_FALSE);
  817. gGL.setColorMask(true, true);
  818. // clear buffer area to ensure we don't pick up UI elements
  819. {
  820. gGL.flush();
  821. LLGLDisable no_alpha(GL_ALPHA_TEST);
  822. if (use_shaders)
  823. {
  824. gAlphaMaskProgram.setMinimumAlpha(0.0f);
  825. }
  826. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  827. gGL.color4f( 0.f, 0.f, 0.f, 1.f );
  828. gl_rect_2d_simple( width, height );
  829. gGL.flush();
  830. if (use_shaders)
  831. {
  832. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  833. }
  834. }
  835. if (mIsVisible)
  836. {
  837. // composite color layers
  838. for( layer_list_t::iterator iter = mLayerList.begin(); iter != mLayerList.end(); iter++ )
  839. {
  840. LLTexLayerInterface* layer = *iter;
  841. if (layer->getRenderPass() == LLTexLayer::RP_COLOR)
  842. {
  843. gGL.flush();
  844. success &= layer->render(x, y, width, height);
  845. gGL.flush();
  846. }
  847. }
  848. renderAlphaMaskTextures(x, y, width, height, false);
  849. stop_glerror();
  850. }
  851. else
  852. {
  853. gGL.flush();
  854. gGL.setSceneBlendType(LLRender::BT_REPLACE);
  855. LLGLDisable no_alpha(GL_ALPHA_TEST);
  856. if (use_shaders)
  857. {
  858. gAlphaMaskProgram.setMinimumAlpha(0.f);
  859. }
  860. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  861. gGL.color4f( 0.f, 0.f, 0.f, 0.f );
  862. gl_rect_2d_simple( width, height );
  863. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  864. gGL.flush();
  865. if (use_shaders)
  866. {
  867. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  868. }
  869. }
  870. return success;
  871. }
  872. BOOL LLTexLayerSet::isBodyRegion(const std::string& region) const
  873. {
  874. return mInfo->mBodyRegion == region;
  875. }
  876. const std::string LLTexLayerSet::getBodyRegionName() const
  877. {
  878. return mInfo->mBodyRegion;
  879. }
  880. void LLTexLayerSet::requestUpdate()
  881. {
  882. if( mUpdatesEnabled )
  883. {
  884. createComposite();
  885. mComposite->requestUpdate();
  886. }
  887. }
  888. void LLTexLayerSet::requestUpload()
  889. {
  890. createComposite();
  891. mComposite->requestUpload();
  892. }
  893. void LLTexLayerSet::cancelUpload()
  894. {
  895. if(mComposite)
  896. {
  897. mComposite->cancelUpload();
  898. }
  899. }
  900. void LLTexLayerSet::createComposite()
  901. {
  902. if(!mComposite)
  903. {
  904. S32 width = mInfo->mWidth;
  905. S32 height = mInfo->mHeight;
  906. // Composite other avatars at reduced resolution
  907. if( !mAvatar->isSelf() )
  908. {
  909. llerrs << "composites should not be created for non-self avatars!" << llendl;
  910. }
  911. mComposite = new LLTexLayerSetBuffer( this, width, height );
  912. }
  913. }
  914. void LLTexLayerSet::destroyComposite()
  915. {
  916. if( mComposite )
  917. {
  918. mComposite = NULL;
  919. }
  920. }
  921. void LLTexLayerSet::setUpdatesEnabled( BOOL b )
  922. {
  923. mUpdatesEnabled = b;
  924. }
  925. void LLTexLayerSet::updateComposite()
  926. {
  927. createComposite();
  928. mComposite->requestUpdateImmediate();
  929. }
  930. LLTexLayerSetBuffer* LLTexLayerSet::getComposite()
  931. {
  932. if (!mComposite)
  933. {
  934. createComposite();
  935. }
  936. return mComposite;
  937. }
  938. const LLTexLayerSetBuffer* LLTexLayerSet::getComposite() const
  939. {
  940. return mComposite;
  941. }
  942. void LLTexLayerSet::gatherMorphMaskAlpha(U8 *data, S32 width, S32 height)
  943. {
  944. memset(data, 255, width * height);
  945. for( layer_list_t::iterator iter = mLayerList.begin(); iter != mLayerList.end(); iter++ )
  946. {
  947. LLTexLayerInterface* layer = *iter;
  948. layer->gatherAlphaMasks(data, mComposite->getOriginX(),mComposite->getOriginY(), width, height);
  949. }
  950. // Set alpha back to that of our alpha masks.
  951. renderAlphaMaskTextures(mComposite->getOriginX(), mComposite->getOriginY(), width, height, true);
  952. }
  953. void LLTexLayerSet::renderAlphaMaskTextures(S32 x, S32 y, S32 width, S32 height, bool forceClear)
  954. {
  955. const LLTexLayerSetInfo *info = getInfo();
  956. bool use_shaders = LLGLSLShader::sNoFixedFunction;
  957. gGL.setColorMask(false, true);
  958. gGL.setSceneBlendType(LLRender::BT_REPLACE);
  959. // (Optionally) replace alpha with a single component image from a tga file.
  960. if (!info->mStaticAlphaFileName.empty())
  961. {
  962. gGL.flush();
  963. {
  964. LLViewerTexture* tex = LLTexLayerStaticImageList::getInstance()->getTexture(info->mStaticAlphaFileName, TRUE);
  965. if( tex )
  966. {
  967. LLGLSUIDefault gls_ui;
  968. gGL.getTexUnit(0)->bind(tex);
  969. gGL.getTexUnit(0)->setTextureBlendType( LLTexUnit::TB_REPLACE );
  970. gl_rect_2d_simple_tex( width, height );
  971. }
  972. }
  973. gGL.flush();
  974. }
  975. else if (forceClear || info->mClearAlpha || (mMaskLayerList.size() > 0))
  976. {
  977. // Set the alpha channel to one (clean up after previous blending)
  978. gGL.flush();
  979. LLGLDisable no_alpha(GL_ALPHA_TEST);
  980. if (use_shaders)
  981. {
  982. gAlphaMaskProgram.setMinimumAlpha(0.f);
  983. }
  984. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  985. gGL.color4f( 0.f, 0.f, 0.f, 1.f );
  986. gl_rect_2d_simple( width, height );
  987. gGL.flush();
  988. if (use_shaders)
  989. {
  990. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  991. }
  992. }
  993. // (Optional) Mask out part of the baked texture with alpha masks
  994. // will still have an effect even if mClearAlpha is set or the alpha component was replaced
  995. if (mMaskLayerList.size() > 0)
  996. {
  997. gGL.setSceneBlendType(LLRender::BT_MULT_ALPHA);
  998. gGL.getTexUnit(0)->setTextureBlendType( LLTexUnit::TB_REPLACE );
  999. for (layer_list_t::iterator iter = mMaskLayerList.begin(); iter != mMaskLayerList.end(); iter++)
  1000. {
  1001. LLTexLayerInterface* layer = *iter;
  1002. gGL.flush();
  1003. layer->blendAlphaTexture(x,y,width, height);
  1004. gGL.flush();
  1005. }
  1006. }
  1007. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1008. gGL.getTexUnit(0)->setTextureBlendType(LLTexUnit::TB_MULT);
  1009. gGL.setColorMask(true, true);
  1010. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  1011. }
  1012. void LLTexLayerSet::applyMorphMask(U8* tex_data, S32 width, S32 height, S32 num_components)
  1013. {
  1014. mAvatar->applyMorphMask(tex_data, width, height, num_components, mBakedTexIndex);
  1015. }
  1016. BOOL LLTexLayerSet::isMorphValid() const
  1017. {
  1018. for(layer_list_t::const_iterator iter = mLayerList.begin(); iter != mLayerList.end(); iter++ )
  1019. {
  1020. const LLTexLayerInterface* layer = *iter;
  1021. if (layer && !layer->isMorphValid())
  1022. {
  1023. return FALSE;
  1024. }
  1025. }
  1026. return TRUE;
  1027. }
  1028. void LLTexLayerSet::invalidateMorphMasks()
  1029. {
  1030. for( layer_list_t::iterator iter = mLayerList.begin(); iter != mLayerList.end(); iter++ )
  1031. {
  1032. LLTexLayerInterface* layer = *iter;
  1033. if (layer)
  1034. {
  1035. layer->invalidateMorphMasks();
  1036. }
  1037. }
  1038. }
  1039. //-----------------------------------------------------------------------------
  1040. // LLTexLayerInfo
  1041. //-----------------------------------------------------------------------------
  1042. LLTexLayerInfo::LLTexLayerInfo() :
  1043. mWriteAllChannels( FALSE ),
  1044. mRenderPass(LLTexLayer::RP_COLOR),
  1045. mFixedColor( 0.f, 0.f, 0.f, 0.f ),
  1046. mLocalTexture( -1 ),
  1047. mStaticImageIsMask( FALSE ),
  1048. mUseLocalTextureAlphaOnly(FALSE),
  1049. mIsVisibilityMask(FALSE)
  1050. {
  1051. }
  1052. LLTexLayerInfo::~LLTexLayerInfo( )
  1053. {
  1054. std::for_each(mParamColorInfoList.begin(), mParamColorInfoList.end(), DeletePointer());
  1055. std::for_each(mParamAlphaInfoList.begin(), mParamAlphaInfoList.end(), DeletePointer());
  1056. }
  1057. BOOL LLTexLayerInfo::parseXml(LLXmlTreeNode* node)
  1058. {
  1059. llassert( node->hasName( "layer" ) );
  1060. // name attribute
  1061. static LLStdStringHandle name_string = LLXmlTree::addAttributeString("name");
  1062. if( !node->getFastAttributeString( name_string, mName ) )
  1063. {
  1064. return FALSE;
  1065. }
  1066. static LLStdStringHandle write_all_channels_string = LLXmlTree::addAttributeString("write_all_channels");
  1067. node->getFastAttributeBOOL( write_all_channels_string, mWriteAllChannels );
  1068. std::string render_pass_name;
  1069. static LLStdStringHandle render_pass_string = LLXmlTree::addAttributeString("render_pass");
  1070. if( node->getFastAttributeString( render_pass_string, render_pass_name ) )
  1071. {
  1072. if( render_pass_name == "bump" )
  1073. {
  1074. mRenderPass = LLTexLayer::RP_BUMP;
  1075. }
  1076. }
  1077. // Note: layers can have either a "global_color" attrib, a "fixed_color" attrib, or a <param_color> child.
  1078. // global color attribute (optional)
  1079. static LLStdStringHandle global_color_string = LLXmlTree::addAttributeString("global_color");
  1080. node->getFastAttributeString( global_color_string, mGlobalColor );
  1081. // Visibility mask (optional)
  1082. BOOL is_visibility;
  1083. static LLStdStringHandle visibility_mask_string = LLXmlTree::addAttributeString("visibility_mask");
  1084. if (node->getFastAttributeBOOL(visibility_mask_string, is_visibility))
  1085. {
  1086. mIsVisibilityMask = is_visibility;
  1087. }
  1088. // color attribute (optional)
  1089. LLColor4U color4u;
  1090. static LLStdStringHandle fixed_color_string = LLXmlTree::addAttributeString("fixed_color");
  1091. if( node->getFastAttributeColor4U( fixed_color_string, color4u ) )
  1092. {
  1093. mFixedColor.setVec( color4u );
  1094. }
  1095. // <texture> optional sub-element
  1096. for (LLXmlTreeNode* texture_node = node->getChildByName( "texture" );
  1097. texture_node;
  1098. texture_node = node->getNextNamedChild())
  1099. {
  1100. std::string local_texture_name;
  1101. static LLStdStringHandle tga_file_string = LLXmlTree::addAttributeString("tga_file");
  1102. static LLStdStringHandle local_texture_string = LLXmlTree::addAttributeString("local_texture");
  1103. static LLStdStringHandle file_is_mask_string = LLXmlTree::addAttributeString("file_is_mask");
  1104. static LLStdStringHandle local_texture_alpha_only_string = LLXmlTree::addAttributeString("local_texture_alpha_only");
  1105. if( texture_node->getFastAttributeString( tga_file_string, mStaticImageFileName ) )
  1106. {
  1107. texture_node->getFastAttributeBOOL( file_is_mask_string, mStaticImageIsMask );
  1108. }
  1109. else if (texture_node->getFastAttributeString(local_texture_string, local_texture_name))
  1110. {
  1111. texture_node->getFastAttributeBOOL( local_texture_alpha_only_string, mUseLocalTextureAlphaOnly );
  1112. /* if ("upper_shirt" == local_texture_name)
  1113. mLocalTexture = TEX_UPPER_SHIRT; */
  1114. mLocalTexture = TEX_NUM_INDICES;
  1115. for (LLVOAvatarDictionary::Textures::const_iterator iter = LLVOAvatarDictionary::getInstance()->getTextures().begin();
  1116. iter != LLVOAvatarDictionary::getInstance()->getTextures().end();
  1117. iter++)
  1118. {
  1119. const LLVOAvatarDictionary::TextureEntry *texture_dict = iter->second;
  1120. if (local_texture_name == texture_dict->mName)
  1121. {
  1122. mLocalTexture = iter->first;
  1123. break;
  1124. }
  1125. }
  1126. if (mLocalTexture == TEX_NUM_INDICES)
  1127. {
  1128. llwarns << "<texture> element has invalid local_texture attribute: " << mName << " " << local_texture_name << llendl;
  1129. return FALSE;
  1130. }
  1131. }
  1132. else
  1133. {
  1134. llwarns << "<texture> element is missing a required attribute. " << mName << llendl;
  1135. return FALSE;
  1136. }
  1137. }
  1138. for (LLXmlTreeNode* maskNode = node->getChildByName( "morph_mask" );
  1139. maskNode;
  1140. maskNode = node->getNextNamedChild())
  1141. {
  1142. std::string morph_name;
  1143. static LLStdStringHandle morph_name_string = LLXmlTree::addAttributeString("morph_name");
  1144. if (maskNode->getFastAttributeString(morph_name_string, morph_name))
  1145. {
  1146. BOOL invert = FALSE;
  1147. static LLStdStringHandle invert_string = LLXmlTree::addAttributeString("invert");
  1148. maskNode->getFastAttributeBOOL(invert_string, invert);
  1149. mMorphNameList.push_back(std::pair<std::string,BOOL>(morph_name,invert));
  1150. }
  1151. }
  1152. // <param> optional sub-element (color or alpha params)
  1153. for (LLXmlTreeNode* child = node->getChildByName( "param" );
  1154. child;
  1155. child = node->getNextNamedChild())
  1156. {
  1157. if( child->getChildByName( "param_color" ) )
  1158. {
  1159. // <param><param_color/></param>
  1160. LLTexLayerParamColorInfo* info = new LLTexLayerParamColorInfo();
  1161. if (!info->parseXml(child))
  1162. {
  1163. delete info;
  1164. return FALSE;
  1165. }
  1166. mParamColorInfoList.push_back(info);
  1167. }
  1168. else if( child->getChildByName( "param_alpha" ) )
  1169. {
  1170. // <param><param_alpha/></param>
  1171. LLTexLayerParamAlphaInfo* info = new LLTexLayerParamAlphaInfo( );
  1172. if (!info->parseXml(child))
  1173. {
  1174. delete info;
  1175. return FALSE;
  1176. }
  1177. mParamAlphaInfoList.push_back(info);
  1178. }
  1179. }
  1180. return TRUE;
  1181. }
  1182. BOOL LLTexLayerInfo::createVisualParams(LLVOAvatar *avatar)
  1183. {
  1184. BOOL success = TRUE;
  1185. for (param_color_info_list_t::iterator color_info_iter = mParamColorInfoList.begin();
  1186. color_info_iter != mParamColorInfoList.end();
  1187. color_info_iter++)
  1188. {
  1189. LLTexLayerParamColorInfo * color_info = *color_info_iter;
  1190. LLTexLayerParamColor* param_color = new LLTexLayerParamColor(avatar);
  1191. if (!param_color->setInfo(color_info, TRUE))
  1192. {
  1193. llwarns << "NULL TexLayer Color Param could not be added to visual param list. Deleting." << llendl;
  1194. delete param_color;
  1195. success = FALSE;
  1196. }
  1197. }
  1198. for (param_alpha_info_list_t::iterator alpha_info_iter = mParamAlphaInfoList.begin();
  1199. alpha_info_iter != mParamAlphaInfoList.end();
  1200. alpha_info_iter++)
  1201. {
  1202. LLTexLayerParamAlphaInfo * alpha_info = *alpha_info_iter;
  1203. LLTexLayerParamAlpha* param_alpha = new LLTexLayerParamAlpha(avatar);
  1204. if (!param_alpha->setInfo(alpha_info, TRUE))
  1205. {
  1206. llwarns << "NULL TexLayer Alpha Param could not be added to visual param list. Deleting." << llendl;
  1207. delete param_alpha;
  1208. success = FALSE;
  1209. }
  1210. }
  1211. return success;
  1212. }
  1213. LLTexLayerInterface::LLTexLayerInterface(LLTexLayerSet* const layer_set):
  1214. mTexLayerSet( layer_set ),
  1215. mMorphMasksValid( FALSE ),
  1216. mInfo(NULL),
  1217. mHasMorph(FALSE)
  1218. {
  1219. }
  1220. LLTexLayerInterface::LLTexLayerInterface(const LLTexLayerInterface &layer, LLWearable *wearable):
  1221. mTexLayerSet( layer.mTexLayerSet ),
  1222. mInfo(NULL)
  1223. {
  1224. // don't add visual params for cloned layers
  1225. setInfo(layer.getInfo(), wearable);
  1226. mHasMorph = layer.mHasMorph;
  1227. }
  1228. BOOL LLTexLayerInterface::setInfo(const LLTexLayerInfo *info, LLWearable* wearable ) // This sets mInfo and calls initialization functions
  1229. {
  1230. // setInfo should only be called once. Code is not robust enough to handle redefinition of a texlayer.
  1231. // Not a critical warning, but could be useful for debugging later issues. -Nyx
  1232. if (mInfo != NULL)
  1233. {
  1234. llwarns << "mInfo != NULL" << llendl;
  1235. }
  1236. mInfo = info;
  1237. //mID = info->mID; // No ID
  1238. mParamColorList.reserve(mInfo->mParamColorInfoList.size());
  1239. for (param_color_info_list_t::const_iterator iter = mInfo->mParamColorInfoList.begin();
  1240. iter != mInfo->mParamColorInfoList.end();
  1241. iter++)
  1242. {
  1243. LLTexLayerParamColor* param_color;
  1244. if (!wearable)
  1245. {
  1246. param_color = new LLTexLayerParamColor(this);
  1247. if (!param_color->setInfo(*iter, TRUE))
  1248. {
  1249. mInfo = NULL;
  1250. return FALSE;
  1251. }
  1252. }
  1253. else
  1254. {
  1255. param_color = (LLTexLayerParamColor*)wearable->getVisualParam((*iter)->getID());
  1256. if (!param_color)
  1257. {
  1258. mInfo = NULL;
  1259. return FALSE;
  1260. }
  1261. }
  1262. mParamColorList.push_back( param_color );
  1263. }
  1264. mParamAlphaList.reserve(mInfo->mParamAlphaInfoList.size());
  1265. for (param_alpha_info_list_t::const_iterator iter = mInfo->mParamAlphaInfoList.begin();
  1266. iter != mInfo->mParamAlphaInfoList.end();
  1267. iter++)
  1268. {
  1269. LLTexLayerParamAlpha* param_alpha;
  1270. if (!wearable)
  1271. {
  1272. param_alpha = new LLTexLayerParamAlpha( this );
  1273. if (!param_alpha->setInfo(*iter, TRUE))
  1274. {
  1275. mInfo = NULL;
  1276. return FALSE;
  1277. }
  1278. }
  1279. else
  1280. {
  1281. param_alpha = (LLTexLayerParamAlpha*) wearable->getVisualParam((*iter)->getID());
  1282. if (!param_alpha)
  1283. {
  1284. mInfo = NULL;
  1285. return FALSE;
  1286. }
  1287. }
  1288. mParamAlphaList.push_back( param_alpha );
  1289. }
  1290. return TRUE;
  1291. }
  1292. /*virtual*/ void LLTexLayerInterface::requestUpdate()
  1293. {
  1294. mTexLayerSet->requestUpdate();
  1295. }
  1296. const std::string& LLTexLayerInterface::getName() const
  1297. {
  1298. return mInfo->mName;
  1299. }
  1300. LLTexLayerInterface::ERenderPass LLTexLayerInterface::getRenderPass() const
  1301. {
  1302. return mInfo->mRenderPass;
  1303. }
  1304. const std::string& LLTexLayerInterface::getGlobalColor() const
  1305. {
  1306. return mInfo->mGlobalColor;
  1307. }
  1308. BOOL LLTexLayerInterface::isVisibilityMask() const
  1309. {
  1310. return mInfo->mIsVisibilityMask;
  1311. }
  1312. void LLTexLayerInterface::invalidateMorphMasks()
  1313. {
  1314. mMorphMasksValid = FALSE;
  1315. }
  1316. LLViewerVisualParam* LLTexLayerInterface::getVisualParamPtr(S32 index) const
  1317. {
  1318. LLViewerVisualParam *result = NULL;
  1319. for (param_color_list_t::const_iterator color_iter = mParamColorList.begin(); color_iter != mParamColorList.end() && !result; ++color_iter)
  1320. {
  1321. if ((*color_iter)->getID() == index)
  1322. {
  1323. result = *color_iter;
  1324. }
  1325. }
  1326. for (param_alpha_list_t::const_iterator alpha_iter = mParamAlphaList.begin(); alpha_iter != mParamAlphaList.end() && !result; ++alpha_iter)
  1327. {
  1328. if ((*alpha_iter)->getID() == index)
  1329. {
  1330. result = *alpha_iter;
  1331. }
  1332. }
  1333. return result;
  1334. }
  1335. //-----------------------------------------------------------------------------
  1336. // LLTexLayer
  1337. // A single texture layer, consisting of:
  1338. // * color, consisting of either
  1339. // * one or more color parameters (weighted colors)
  1340. // * a reference to a global color
  1341. // * a fixed color with non-zero alpha
  1342. // * opaque white (the default)
  1343. // * (optional) a texture defined by either
  1344. // * a GUID
  1345. // * a texture entry index (TE)
  1346. // * (optional) one or more alpha parameters (weighted alpha textures)
  1347. //-----------------------------------------------------------------------------
  1348. LLTexLayer::LLTexLayer(LLTexLayerSet* const layer_set) :
  1349. LLTexLayerInterface( layer_set ),
  1350. mLocalTextureObject(NULL)
  1351. {
  1352. }
  1353. LLTexLayer::LLTexLayer(const LLTexLayer &layer, LLWearable *wearable) :
  1354. LLTexLayerInterface( layer, wearable ),
  1355. mLocalTextureObject(NULL)
  1356. {
  1357. }
  1358. LLTexLayer::LLTexLayer(const LLTexLayerTemplate &layer_template, LLLocalTextureObject *lto, LLWearable *wearable) :
  1359. LLTexLayerInterface( layer_template, wearable ),
  1360. mLocalTextureObject(lto)
  1361. {
  1362. }
  1363. LLTexLayer::~LLTexLayer()
  1364. {
  1365. // mParamAlphaList and mParamColorList are LLViewerVisualParam's and get
  1366. // deleted with ~LLCharacter()
  1367. //std::for_each(mParamAlphaList.begin(), mParamAlphaList.end(), DeletePointer());
  1368. //std::for_each(mParamColorList.begin(), mParamColorList.end(), DeletePointer());
  1369. for( alpha_cache_t::iterator iter = mAlphaCache.begin();
  1370. iter != mAlphaCache.end(); iter++ )
  1371. {
  1372. U8* alpha_data = iter->second;
  1373. delete [] alpha_data;
  1374. }
  1375. }
  1376. //-----------------------------------------------------------------------------
  1377. // setInfo
  1378. //-----------------------------------------------------------------------------
  1379. BOOL LLTexLayer::setInfo(const LLTexLayerInfo* info, LLWearable* wearable )
  1380. {
  1381. return LLTexLayerInterface::setInfo(info, wearable);
  1382. }
  1383. //static
  1384. void LLTexLayer::calculateTexLayerColor(const param_color_list_t &param_list, LLColor4 &net_color)
  1385. {
  1386. for (param_color_list_t::const_iterator iter = param_list.begin();
  1387. iter != param_list.end(); iter++)
  1388. {
  1389. const LLTexLayerParamColor* param = *iter;
  1390. LLColor4 param_net = param->getNetColor();
  1391. const LLTexLayerParamColorInfo *info = (LLTexLayerParamColorInfo *)param->getInfo();
  1392. switch(info->getOperation())
  1393. {
  1394. case LLTexLayerParamColor::OP_ADD:
  1395. net_color += param_net;
  1396. break;
  1397. case LLTexLayerParamColor::OP_MULTIPLY:
  1398. net_color = net_color * param_net;
  1399. break;
  1400. case LLTexLayerParamColor::OP_BLEND:
  1401. net_color = lerp(net_color, param_net, param->getWeight());
  1402. break;
  1403. default:
  1404. llassert(0);
  1405. break;
  1406. }
  1407. }
  1408. net_color.clamp();
  1409. }
  1410. /*virtual*/ void LLTexLayer::deleteCaches()
  1411. {
  1412. // Only need to delete caches for alpha params. Color params don't hold extra memory
  1413. for (param_alpha_list_t::iterator iter = mParamAlphaList.begin();
  1414. iter != mParamAlphaList.end(); iter++ )
  1415. {
  1416. LLTexLayerParamAlpha* param = *iter;
  1417. param->deleteCaches();
  1418. }
  1419. }
  1420. BOOL LLTexLayer::render(S32 x, S32 y, S32 width, S32 height)
  1421. {
  1422. LLGLEnable color_mat(GL_COLOR_MATERIAL);
  1423. gPipeline.disableLights();
  1424. bool use_shaders = LLGLSLShader::sNoFixedFunction;
  1425. LLColor4 net_color;
  1426. BOOL color_specified = findNetColor(&net_color);
  1427. if (mTexLayerSet->getAvatar()->mIsDummy)
  1428. {
  1429. color_specified = true;
  1430. net_color = LLVOAvatar::getDummyColor();
  1431. }
  1432. BOOL success = TRUE;
  1433. // If you can't see the layer, don't render it.
  1434. if( is_approx_zero( net_color.mV[VW] ) )
  1435. {
  1436. return success;
  1437. }
  1438. BOOL alpha_mask_specified = FALSE;
  1439. param_alpha_list_t::const_iterator iter = mParamAlphaList.begin();
  1440. if( iter != mParamAlphaList.end() )
  1441. {
  1442. // If we have alpha masks, but we're skipping all of them, skip the whole layer.
  1443. // However, we can't do this optimization if we have morph masks that need updating.
  1444. /* if (!mHasMorph)
  1445. {
  1446. BOOL skip_layer = TRUE;
  1447. while( iter != mParamAlphaList.end() )
  1448. {
  1449. const LLTexLayerParamAlpha* param = *iter;
  1450. if( !param->getSkip() )
  1451. {
  1452. skip_layer = FALSE;
  1453. break;
  1454. }
  1455. iter++;
  1456. }
  1457. if( skip_layer )
  1458. {
  1459. return success;
  1460. }
  1461. }//*/
  1462. renderMorphMasks(x, y, width, height, net_color);
  1463. alpha_mask_specified = TRUE;
  1464. gGL.flush();
  1465. gGL.blendFunc(LLRender::BF_DEST_ALPHA, LLRender::BF_ONE_MINUS_DEST_ALPHA);
  1466. }
  1467. gGL.color4fv( net_color.mV);
  1468. if( getInfo()->mWriteAllChannels )
  1469. {
  1470. gGL.flush();
  1471. gGL.setSceneBlendType(LLRender::BT_REPLACE);
  1472. }
  1473. if( (getInfo()->mLocalTexture != -1) && !getInfo()->mUseLocalTextureAlphaOnly )
  1474. {
  1475. {
  1476. LLViewerTexture* tex = NULL;
  1477. if (mLocalTextureObject && mLocalTextureObject->getImage())
  1478. {
  1479. tex = mLocalTextureObject->getImage();
  1480. if (mLocalTextureObject->getID() == IMG_DEFAULT_AVATAR)
  1481. {
  1482. tex = NULL;
  1483. }
  1484. }
  1485. else
  1486. {
  1487. llinfos << "lto not defined or image not defined: " << getInfo()->getLocalTexture() << " lto: " << mLocalTextureObject << llendl;
  1488. }
  1489. // if( mTexLayerSet->getAvatar()->getLocalTextureGL((ETextureIndex)getInfo()->mLocalTexture, &image_gl ) )
  1490. {
  1491. if( tex )
  1492. {
  1493. bool no_alpha_test = getInfo()->mWriteAllChannels;
  1494. LLGLDisable alpha_test(no_alpha_test ? GL_ALPHA_TEST : 0);
  1495. if (use_shaders && no_alpha_test)
  1496. {
  1497. gAlphaMaskProgram.setMinimumAlpha(0.f);
  1498. }
  1499. LLTexUnit::eTextureAddressMode old_mode = tex->getAddressMode();
  1500. gGL.getTexUnit(0)->bind(tex, TRUE);
  1501. gGL.getTexUnit(0)->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  1502. gl_rect_2d_simple_tex( width, height );
  1503. gGL.getTexUnit(0)->setTextureAddressMode(old_mode);
  1504. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1505. if (use_shaders && no_alpha_test)
  1506. {
  1507. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  1508. }
  1509. }
  1510. }
  1511. // else
  1512. // {
  1513. // success = FALSE;
  1514. // }
  1515. }
  1516. }
  1517. if( !getInfo()->mStaticImageFileName.empty() )
  1518. {
  1519. {
  1520. LLViewerTexture* tex = LLTexLayerStaticImageList::getInstance()->getTexture(getInfo()->mStaticImageFileName, getInfo()->mStaticImageIsMask);
  1521. if( tex )
  1522. {
  1523. gGL.getTexUnit(0)->bind(tex, TRUE);
  1524. gl_rect_2d_simple_tex( width, height );
  1525. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1526. }
  1527. else
  1528. {
  1529. success = FALSE;
  1530. }
  1531. }
  1532. }
  1533. if(((-1 == getInfo()->mLocalTexture) ||
  1534. getInfo()->mUseLocalTextureAlphaOnly) &&
  1535. getInfo()->mStaticImageFileName.empty() &&
  1536. color_specified )
  1537. {
  1538. LLGLDisable no_alpha(GL_ALPHA_TEST);
  1539. if (use_shaders)
  1540. {
  1541. gAlphaMaskProgram.setMinimumAlpha(0.f);
  1542. }
  1543. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1544. gGL.color4fv( net_color.mV );
  1545. gl_rect_2d_simple( width, height );
  1546. if (use_shaders)
  1547. {
  1548. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  1549. }
  1550. }
  1551. if( alpha_mask_specified || getInfo()->mWriteAllChannels )
  1552. {
  1553. // Restore standard blend func value
  1554. gGL.flush();
  1555. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  1556. stop_glerror();
  1557. }
  1558. if( !success )
  1559. {
  1560. llinfos << "LLTexLayer::render() partial: " << getInfo()->mName << llendl;
  1561. }
  1562. return success;
  1563. }
  1564. const U8* LLTexLayer::getAlphaData() const
  1565. {
  1566. LLCRC alpha_mask_crc;
  1567. const LLUUID& uuid = getUUID();
  1568. alpha_mask_crc.update((U8*)(&uuid.mData), UUID_BYTES);
  1569. for (param_alpha_list_t::const_iterator iter = mParamAlphaList.begin(); iter != mParamAlphaList.end(); iter++)
  1570. {
  1571. const LLTexLayerParamAlpha* param = *iter;
  1572. // MULTI-WEARABLE: verify visual parameters used here
  1573. F32 param_weight = param->getWeight();
  1574. alpha_mask_crc.update((U8*)&param_weight, sizeof(F32));
  1575. }
  1576. U32 cache_index = alpha_mask_crc.getCRC();
  1577. alpha_cache_t::const_iterator iter2 = mAlphaCache.find(cache_index);
  1578. return (iter2 == mAlphaCache.end()) ? 0 : iter2->second;
  1579. }
  1580. BOOL LLTexLayer::findNetColor(LLColor4* net_color) const
  1581. {
  1582. // Color is either:
  1583. // * one or more color parameters (weighted colors) (which may make use of a global color or fixed color)
  1584. // * a reference to a global color
  1585. // * a fixed color with non-zero alpha
  1586. // * opaque white (the default)
  1587. if( !mParamColorList.empty() )
  1588. {
  1589. if( !getGlobalColor().empty() )
  1590. {
  1591. net_color->setVec( mTexLayerSet->getAvatar()->getGlobalColor( getInfo()->mGlobalColor ) );
  1592. }
  1593. else if (getInfo()->mFixedColor.mV[VW])
  1594. {
  1595. net_color->setVec( getInfo()->mFixedColor );
  1596. }
  1597. else
  1598. {
  1599. net_color->setVec( 0.f, 0.f, 0.f, 0.f );
  1600. }
  1601. calculateTexLayerColor(mParamColorList, *net_color);
  1602. return TRUE;
  1603. }
  1604. if( !getGlobalColor().empty() )
  1605. {
  1606. net_color->setVec( mTexLayerSet->getAvatar()->getGlobalColor( getGlobalColor() ) );
  1607. return TRUE;
  1608. }
  1609. if( getInfo()->mFixedColor.mV[VW] )
  1610. {
  1611. net_color->setVec( getInfo()->mFixedColor );
  1612. return TRUE;
  1613. }
  1614. net_color->setToWhite();
  1615. return FALSE; // No need to draw a separate colored polygon
  1616. }
  1617. BOOL LLTexLayer::blendAlphaTexture(S32 x, S32 y, S32 width, S32 height)
  1618. {
  1619. BOOL success = TRUE;
  1620. gGL.flush();
  1621. bool use_shaders = LLGLSLShader::sNoFixedFunction;
  1622. if( !getInfo()->mStaticImageFileName.empty() )
  1623. {
  1624. LLViewerTexture* tex = LLTexLayerStaticImageList::getInstance()->getTexture( getInfo()->mStaticImageFileName, getInfo()->mStaticImageIsMask );
  1625. if( tex )
  1626. {
  1627. LLGLSNoAlphaTest gls_no_alpha_test;
  1628. if (use_shaders)
  1629. {
  1630. gAlphaMaskProgram.setMinimumAlpha(0.f);
  1631. }
  1632. gGL.getTexUnit(0)->bind(tex, TRUE);
  1633. gl_rect_2d_simple_tex( width, height );
  1634. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1635. if (use_shaders)
  1636. {
  1637. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  1638. }
  1639. }
  1640. else
  1641. {
  1642. success = FALSE;
  1643. }
  1644. }
  1645. else
  1646. {
  1647. if (getInfo()->mLocalTexture >=0 && getInfo()->mLocalTexture < TEX_NUM_INDICES)
  1648. {
  1649. LLViewerTexture* tex = mLocalTextureObject->getImage();
  1650. if (tex)
  1651. {
  1652. LLGLSNoAlphaTest gls_no_alpha_test;
  1653. if (use_shaders)
  1654. {
  1655. gAlphaMaskProgram.setMinimumAlpha(0.f);
  1656. }
  1657. gGL.getTexUnit(0)->bind(tex);
  1658. gl_rect_2d_simple_tex( width, height );
  1659. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1660. success = TRUE;
  1661. if (use_shaders)
  1662. {
  1663. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  1664. }
  1665. }
  1666. }
  1667. }
  1668. return success;
  1669. }
  1670. /*virtual*/ void LLTexLayer::gatherAlphaMasks(U8 *data, S32 originX, S32 originY, S32 width, S32 height)
  1671. {
  1672. addAlphaMask(data, originX, originY, width, height);
  1673. }
  1674. BOOL LLTexLayer::renderMorphMasks(S32 x, S32 y, S32 width, S32 height, const LLColor4 &layer_color)
  1675. {
  1676. BOOL success = TRUE;
  1677. llassert( !mParamAlphaList.empty() );
  1678. bool use_shaders = LLGLSLShader::sNoFixedFunction;
  1679. if (use_shaders)
  1680. {
  1681. gAlphaMaskProgram.setMinimumAlpha(0.f);
  1682. }
  1683. gGL.setColorMask(false, true);
  1684. LLTexLayerParamAlpha* first_param = *mParamAlphaList.begin();
  1685. // Note: if the first param is a mulitply, multiply against the current buffer's alpha
  1686. if( !first_param || !first_param->getMultiplyBlend() )
  1687. {
  1688. LLGLDisable no_alpha(GL_ALPHA_TEST);
  1689. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1690. // Clear the alpha
  1691. gGL.flush();
  1692. gGL.setSceneBlendType(LLRender::BT_REPLACE);
  1693. gGL.color4f( 0.f, 0.f, 0.f, 0.f );
  1694. gl_rect_2d_simple( width, height );
  1695. }
  1696. // Accumulate alphas
  1697. LLGLSNoAlphaTest gls_no_alpha_test;
  1698. gGL.color4f( 1.f, 1.f, 1.f, 1.f );
  1699. for (param_alpha_list_t::iterator iter = mParamAlphaList.begin(); iter != mParamAlphaList.end(); iter++)
  1700. {
  1701. LLTexLayerParamAlpha* param = *iter;
  1702. success &= param->render( x, y, width, height );
  1703. }
  1704. // Approximates a min() function
  1705. gGL.flush();
  1706. gGL.setSceneBlendType(LLRender::BT_MULT_ALPHA);
  1707. // Accumulate the alpha component of the texture
  1708. if( getInfo()->mLocalTexture != -1 )
  1709. {
  1710. LLViewerTexture* tex = mLocalTextureObject->getImage();
  1711. if( tex && (tex->getComponents() == 4) )
  1712. {
  1713. LLGLSNoAlphaTest gls_no_alpha_test;
  1714. LLTexUnit::eTextureAddressMode old_mode = tex->getAddressMode();
  1715. gGL.getTexUnit(0)->bind(tex, TRUE);
  1716. gGL.getTexUnit(0)->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  1717. gl_rect_2d_simple_tex( width, height );
  1718. gGL.getTexUnit(0)->setTextureAddressMode(old_mode);
  1719. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1720. }
  1721. }
  1722. if( !getInfo()->mStaticImageFileName.empty() )
  1723. {
  1724. LLViewerTexture* tex = LLTexLayerStaticImageList::getInstance()->getTexture(getInfo()->mStaticImageFileName, getInfo()->mStaticImageIsMask);
  1725. if( tex )
  1726. {
  1727. if( (tex->getComponents() == 4) ||
  1728. ( (tex->getComponents() == 1) && getInfo()->mStaticImageIsMask ) )
  1729. {
  1730. LLGLSNoAlphaTest gls_no_alpha_test;
  1731. gGL.getTexUnit(0)->bind(tex, TRUE);
  1732. gl_rect_2d_simple_tex( width, height );
  1733. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1734. }
  1735. }
  1736. }
  1737. // Draw a rectangle with the layer color to multiply the alpha by that color's alpha.
  1738. // Note: we're still using gGL.blendFunc( GL_DST_ALPHA, GL_ZERO );
  1739. if (layer_color.mV[VW] != 1.f)
  1740. {
  1741. LLGLDisable no_alpha(GL_ALPHA_TEST);
  1742. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1743. gGL.color4fv(layer_color.mV);
  1744. gl_rect_2d_simple( width, height );
  1745. }
  1746. if (use_shaders)
  1747. {
  1748. gAlphaMaskProgram.setMinimumAlpha(0.004f);
  1749. }
  1750. LLGLSUIDefault gls_ui;
  1751. gGL.setColorMask(true, true);
  1752. if (hasMorph() && success)
  1753. {
  1754. LLCRC alpha_mask_crc;
  1755. const LLUUID& uuid = getUUID();
  1756. alpha_mask_crc.update((U8*)(&uuid.mData), UUID_BYTES);
  1757. for (param_alpha_list_t::const_iterator iter = mParamAlphaList.begin(); iter != mParamAlphaList.end(); iter++)
  1758. {
  1759. const LLTexLayerParamAlpha* param = *iter;
  1760. F32 param_weight = param->getWeight();
  1761. alpha_mask_crc.update((U8*)&param_weight, sizeof(F32));
  1762. }
  1763. U32 cache_index = alpha_mask_crc.getCRC();
  1764. U8* alpha_data = get_if_there(mAlphaCache,cache_index,(U8*)NULL);
  1765. if (!alpha_data)
  1766. {
  1767. // clear out a slot if we have filled our cache
  1768. S32 max_cache_entries = getTexLayerSet()->getAvatar()->isSelf() ? 4 : 1;
  1769. while ((S32)mAlphaCache.size() >= max_cache_entries)
  1770. {
  1771. alpha_cache_t::iterator iter2 = mAlphaCache.begin(); // arbitrarily grab the first entry
  1772. alpha_data = iter2->second;
  1773. delete [] alpha_data;
  1774. mAlphaCache.erase(iter2);
  1775. }
  1776. alpha_data = new U8[width * height];
  1777. mAlphaCache[cache_index] = alpha_data;
  1778. glReadPixels(x, y, width, height, GL_ALPHA, GL_UNSIGNED_BYTE, alpha_data);
  1779. }
  1780. getTexLayerSet()->getAvatar()->dirtyMesh();
  1781. mMorphMasksValid = TRUE;
  1782. getTexLayerSet()->applyMorphMask(alpha_data, width, height, 1);
  1783. }
  1784. return success;
  1785. }
  1786. void LLTexLayer::addAlphaMask(U8 *data, S32 originX, S32 originY, S32 width, S32 height)
  1787. {
  1788. S32 size = width * height;
  1789. const U8* alphaData = getAlphaData();
  1790. if (!alphaData && hasAlphaParams())
  1791. {
  1792. LLColor4 net_color;
  1793. findNetColor( &net_color );
  1794. // TODO: eliminate need for layer morph mask valid flag
  1795. invalidateMorphMasks();
  1796. renderMorphMasks(originX, originY, width, height, net_color);
  1797. alphaData = getAlphaData();
  1798. }
  1799. if (alphaData)
  1800. {
  1801. for( S32 i = 0; i < size; i++ )
  1802. {
  1803. U8 curAlpha = data[i];
  1804. U16 resultAlpha = curAlpha;
  1805. resultAlpha *= (alphaData[i] + 1);
  1806. resultAlpha = resultAlpha >> 8;
  1807. data[i] = (U8)resultAlpha;
  1808. }
  1809. }
  1810. }
  1811. /*virtual*/ BOOL LLTexLayer::isInvisibleAlphaMask() const
  1812. {
  1813. if (mLocalTextureObject)
  1814. {
  1815. if (mLocalTextureObject->getID() == IMG_INVISIBLE)
  1816. {
  1817. return TRUE;
  1818. }
  1819. }
  1820. return FALSE;
  1821. }
  1822. LLUUID LLTexLayer::getUUID() const
  1823. {
  1824. LLUUID uuid;
  1825. if( getInfo()->mLocalTexture != -1 )
  1826. {
  1827. LLViewerTexture* tex = mLocalTextureObject->getImage();
  1828. if (tex)
  1829. {
  1830. uuid = mLocalTextureObject->getID();
  1831. }
  1832. }
  1833. if( !getInfo()->mStaticImageFileName.empty() )
  1834. {
  1835. LLViewerTexture* tex = LLTexLayerStaticImageList::getInstance()->getTexture(getInfo()->mStaticImageFileName, getInfo()->mStaticImageIsMask);
  1836. if( tex )
  1837. {
  1838. uuid = tex->getID();
  1839. }
  1840. }
  1841. return uuid;
  1842. }
  1843. //-----------------------------------------------------------------------------
  1844. // LLTexLayerTemplate
  1845. // A single texture layer, consisting of:
  1846. // * color, consisting of either
  1847. // * one or more color parameters (weighted colors)
  1848. // * a reference to a global color
  1849. // * a fixed color with non-zero alpha
  1850. // * opaque white (the default)
  1851. // * (optional) a texture defined by either
  1852. // * a GUID
  1853. // * a texture entry index (TE)
  1854. // * (optional) one or more alpha parameters (weighted alpha textures)
  1855. //-----------------------------------------------------------------------------
  1856. LLTexLayerTemplate::LLTexLayerTemplate(LLTexLayerSet* layer_set) :
  1857. LLTexLayerInterface(layer_set)
  1858. {
  1859. }
  1860. LLTexLayerTemplate::LLTexLayerTemplate(const LLTexLayerTemplate &layer) :
  1861. LLTexLayerInterface(layer)
  1862. {
  1863. }
  1864. LLTexLayerTemplate::~LLTexLayerTemplate()
  1865. {
  1866. }
  1867. //-----------------------------------------------------------------------------
  1868. // setInfo
  1869. //-----------------------------------------------------------------------------
  1870. /*virtual*/ BOOL LLTexLayerTemplate::setInfo(const LLTexLayerInfo* info, LLWearable* wearable )
  1871. {
  1872. return LLTexLayerInterface::setInfo(info, wearable);
  1873. }
  1874. U32 LLTexLayerTemplate::updateWearableCache() const
  1875. {
  1876. mWearableCache.clear();
  1877. S32 te = mInfo->mLocalTexture;
  1878. if (te == -1)
  1879. {
  1880. //this isn't a cloneable layer
  1881. return 0;
  1882. }
  1883. LLWearableType::EType wearable_type = LLVOAvatarDictionary::getTEWearableType((ETextureIndex)te);
  1884. U32 num_wearables = gAgentWearables.getWearableCount(wearable_type);
  1885. U32 added = 0;
  1886. for (U32 i = 0; i < num_wearables; i++)
  1887. {
  1888. LLWearable* wearable = gAgentWearables.getWearable(wearable_type, i);
  1889. if (!wearable)
  1890. {
  1891. continue;
  1892. }
  1893. mWearableCache.push_back(wearable);
  1894. added++;
  1895. }
  1896. return added;
  1897. }
  1898. LLTexLayer* LLTexLayerTemplate::getLayer(U32 i) const
  1899. {
  1900. if (mWearableCache.size() <= i)
  1901. {
  1902. return NULL;
  1903. }
  1904. LLWearable *wearable = mWearableCache[i];
  1905. LLLocalTextureObject *lto = NULL;
  1906. LLTexLayer *layer = NULL;
  1907. if (wearable)
  1908. {
  1909. lto = wearable->getLocalTextureObject(mInfo->mLocalTexture);
  1910. }
  1911. if (lto)
  1912. {
  1913. layer = lto->getTexLayer(getName());
  1914. }
  1915. return layer;
  1916. }
  1917. /*virtual*/ BOOL LLTexLayerTemplate::render(S32 x, S32 y, S32 width, S32 height)
  1918. {
  1919. if(!mInfo)
  1920. {
  1921. return FALSE ;
  1922. }
  1923. BOOL success = TRUE;
  1924. updateWearableCache();
  1925. for (wearable_cache_t::const_iterator iter = mWearableCache.begin(); iter!= mWearableCache.end(); iter++)
  1926. {
  1927. LLWearable* wearable = NULL;
  1928. LLLocalTextureObject *lto = NULL;
  1929. LLTexLayer *layer = NULL;
  1930. wearable = *iter;
  1931. if (wearable)
  1932. {
  1933. lto = wearable->getLocalTextureObject(mInfo->mLocalTexture);
  1934. }
  1935. if (lto)
  1936. {
  1937. layer = lto->getTexLayer(getName());
  1938. }
  1939. if (layer)
  1940. {
  1941. wearable->writeToAvatar();
  1942. layer->setLTO(lto);
  1943. success &= layer->render(x,y,width,height);
  1944. }
  1945. }
  1946. return success;
  1947. }
  1948. /*virtual*/ BOOL LLTexLayerTemplate::blendAlphaTexture( S32 x, S32 y, S32 width, S32 height) // Multiplies a single alpha texture against the frame buffer
  1949. {
  1950. BOOL success = TRUE;
  1951. U32 num_wearables = updateWearableCache();
  1952. for (U32 i = 0; i < num_wearables; i++)
  1953. {
  1954. LLTexLayer *layer = getLayer(i);
  1955. if (layer)
  1956. {
  1957. success &= layer->blendAlphaTexture(x,y,width,height);
  1958. }
  1959. }
  1960. return success;
  1961. }
  1962. /*virtual*/ void LLTexLayerTemplate::gatherAlphaMasks(U8 *data, S32 originX, S32 originY, S32 width, S32 height)
  1963. {
  1964. U32 num_wearables = updateWearableCache();
  1965. for (U32 i = 0; i < num_wearables; i++)
  1966. {
  1967. LLTexLayer *layer = getLayer(i);
  1968. if (layer)
  1969. {
  1970. layer->addAlphaMask(data, originX, originY, width, height);
  1971. }
  1972. }
  1973. }
  1974. /*virtual*/ void LLTexLayerTemplate::setHasMorph(BOOL newval)
  1975. {
  1976. mHasMorph = newval;
  1977. U32 num_wearables = updateWearableCache();
  1978. for (U32 i = 0; i < num_wearables; i++)
  1979. {
  1980. LLTexLayer *layer = getLayer(i);
  1981. if (layer)
  1982. {
  1983. layer->setHasMorph(newval);
  1984. }
  1985. }
  1986. }
  1987. /*virtual*/ void LLTexLayerTemplate::deleteCaches()
  1988. {
  1989. U32 num_wearables = updateWearableCache();
  1990. for (U32 i = 0; i < num_wearables; i++)
  1991. {
  1992. LLTexLayer *layer = getLayer(i);
  1993. if (layer)
  1994. {
  1995. layer->deleteCaches();
  1996. }
  1997. }
  1998. }
  1999. /*virtual*/ BOOL LLTexLayerTemplate::isInvisibleAlphaMask() const
  2000. {
  2001. U32 num_wearables = updateWearableCache();
  2002. for (U32 i = 0; i < num_wearables; i++)
  2003. {
  2004. LLTexLayer *layer = getLayer(i);
  2005. if (layer)
  2006. {
  2007. if (layer->isInvisibleAlphaMask())
  2008. {
  2009. return TRUE;
  2010. }
  2011. }
  2012. }
  2013. return FALSE;
  2014. }
  2015. //-----------------------------------------------------------------------------
  2016. // finds a specific layer based on a passed in name
  2017. //-----------------------------------------------------------------------------
  2018. LLTexLayerInterface* LLTexLayerSet::findLayerByName(const std::string& name)
  2019. {
  2020. for (layer_list_t::iterator iter = mLayerList.begin(); iter != mLayerList.end(); iter++ )
  2021. {
  2022. LLTexLayerInterface* layer = *iter;
  2023. if (layer->getName() == name)
  2024. {
  2025. return layer;
  2026. }
  2027. }
  2028. for (layer_list_t::iterator iter = mMaskLayerList.begin(); iter != mMaskLayerList.end(); iter++ )
  2029. {
  2030. LLTexLayerInterface* layer = *iter;
  2031. if (layer->getName() == name)
  2032. {
  2033. return layer;
  2034. }
  2035. }
  2036. return NULL;
  2037. }
  2038. void LLTexLayerSet::cloneTemplates(LLLocalTextureObject *lto, LLVOAvatarDefines::ETextureIndex tex_index, LLWearable *wearable)
  2039. {
  2040. // initialize all texlayers with this texture type for this LTO
  2041. for( LLTexLayerSet::layer_list_t::iterator iter = mLayerList.begin(); iter != mLayerList.end(); iter++ )
  2042. {
  2043. LLTexLayerTemplate* layer = (LLTexLayerTemplate*)*iter;
  2044. if (layer->getInfo()->getLocalTexture() == (S32) tex_index)
  2045. {
  2046. lto->addTexLayer(layer, wearable);
  2047. }
  2048. }
  2049. for( LLTexLayerSet::layer_list_t::iterator iter = mMaskLayerList.begin(); iter != mMaskLayerList.end(