llface.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /**
  2. * @file llface.h
  3. * @brief LLFace class definition
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLFACE_H
  33. #define LL_LLFACE_H
  34. #include "llmodel.h"
  35. #include "llrender.h"
  36. #include "llstrider.h"
  37. #include "llvertexbuffer.h"
  38. #include "llcolor4u.h"
  39. #include "lldrawable.h"
  40. #include "llviewertexture.h"
  41. // Defined to 1 to reinstate the putative fix for MAINT-4773/SL-5842 i.e.
  42. // "transparent alpha being white" in some materials: this fix got reverted
  43. // in LL's PBR viewer due to "critical flaw of the fix replacing material
  44. // (sometimes server side included) and ignoring user and script input in some
  45. // cases that makes scripts misbehave". However, without this fix, some rigged
  46. // meshes disappear when in forward rendering mode. See this forum post:
  47. // http://sldev.free.fr/forum/viewtopic.php?f=4&p=11950#p11950 - HB
  48. #define LL_FIX_MAT_TRANSPARENCY 1
  49. class LLDrawInfo;
  50. class LLFacePool;
  51. class LLGeometryManager;
  52. class LLTextureEntry;
  53. class LLVertexProgram;
  54. class LLViewerTexture;
  55. class LLVolume;
  56. constexpr F32 MIN_ALPHA_SIZE = 1024.f;
  57. constexpr F32 MIN_TEX_ANIM_SIZE = 512.f;
  58. constexpr U8 FACE_DO_NOT_BATCH_TEXTURES = 255;
  59. class LLFace
  60. {
  61. protected:
  62. LOG_CLASS(LLFace);
  63. public:
  64. LL_ALIGNED16_NEW_DELETE
  65. LLFace(const LLFace& rhs)
  66. {
  67. *this = rhs;
  68. }
  69. const LLFace& operator=(const LLFace& rhs)
  70. {
  71. llerrs << "Illegal operation !" << llendl;
  72. return *this;
  73. }
  74. enum EMasks
  75. {
  76. LIGHT = 0x0001,
  77. GLOBAL = 0x0002,
  78. FULLBRIGHT = 0x0004,
  79. HUD_RENDER = 0x0008,
  80. USE_FACE_COLOR = 0x0010,
  81. TEXTURE_ANIM = 0x0020,
  82. RIGGED = 0x0040,
  83. //MK
  84. USE_RLV_TEXTURE = 0x0080,
  85. //mk
  86. };
  87. public:
  88. LL_INLINE LLFace(LLDrawable* drawablep, LLViewerObject* objp)
  89. {
  90. init(drawablep, objp);
  91. }
  92. LL_INLINE ~LLFace() { destroy(); }
  93. LL_INLINE const LLMatrix4& getWorldMatrix() const { return mVObjp->getWorldMatrix(mXform); }
  94. const LLMatrix4& getRenderMatrix() const;
  95. LL_INLINE U32 getIndicesCount() const { return mIndicesCount; }
  96. LL_INLINE S32 getIndicesStart() const { return mIndicesIndex; }
  97. // Vertex count for this face
  98. LL_INLINE U16 getGeomCount() const { return mGeomCount; }
  99. // Index into draw pool
  100. LL_INLINE U16 getGeomIndex() const { return mGeomIndex; }
  101. // Index into draw pool
  102. LL_INLINE U16 getGeomStart() const { return mGeomIndex; }
  103. void setTextureIndex(U8 index);
  104. LL_INLINE U8 getTextureIndex() const { return mTextureIndex; }
  105. void setTexture(U32 ch, LLViewerTexture* texp);
  106. LL_INLINE void setDiffuseMap(LLViewerTexture* texp)
  107. {
  108. setTexture(LLRender::DIFFUSE_MAP, texp);
  109. }
  110. LL_INLINE void setNormalMap(LLViewerTexture* texp)
  111. {
  112. setTexture(LLRender::NORMAL_MAP, texp);
  113. }
  114. LL_INLINE void setSpecularMap(LLViewerTexture* texp)
  115. {
  116. setTexture(LLRender::SPECULAR_MAP, texp);
  117. }
  118. // Used to switch between diffuse and media textures.
  119. void switchTexture(U32 ch, LLViewerTexture* texp);
  120. // Used to switch between diffuse and base color textures. HB
  121. void switchDiffuseTex(const LLUUID& tex_id);
  122. void dirtyTexture();
  123. #if LL_FIX_MAT_TRANSPARENCY
  124. void notifyAboutCreatingTexture(LLViewerTexture* texp);
  125. void notifyAboutMissingAsset(LLViewerTexture* texp);
  126. #endif
  127. // Used to preserve draw order of faces that are batched together. Allows
  128. // content creators to manipulate linked sets and face ordering for
  129. // consistent alpha sorting results, particularly for rigged attachments.
  130. LL_INLINE void setDrawOrderIndex(U32 index) { mDrawOrderIndex = index; }
  131. LL_INLINE U32 getDrawOrderIndex() const { return mDrawOrderIndex; }
  132. LL_INLINE LLXformMatrix* getXform() const { return mXform; }
  133. LL_INLINE bool hasGeometry() const { return mGeomCount > 0; }
  134. LLVector3 getPositionAgent() const;
  135. LLVector2 surfaceToTexture(LLVector2 surface_coord, const LLVector4a& pos,
  136. const LLVector4a& normal);
  137. void getPlanarProjectedParams(LLQuaternion* face_rot, LLVector3* face_pos,
  138. F32* scale) const;
  139. bool calcAlignedPlanarTE(const LLFace* align_to, LLVector2* st_offset,
  140. LLVector2* st_scale, F32* st_rot,
  141. S32 map = LLRender::DIFFUSE_MAP) const;
  142. LL_INLINE U32 getState() const { return mState; }
  143. LL_INLINE void setState(U32 state) { mState |= state; }
  144. LL_INLINE void clearState(U32 state) { mState &= ~state; }
  145. LL_INLINE bool isState(U32 state) const { return (mState & state) != 0; }
  146. LL_INLINE void setVirtualSize(F32 size) { mVSize = size; }
  147. LL_INLINE void setPixelArea(F32 area) { mPixelArea = area; }
  148. LL_INLINE F32 getVirtualSize() const { return mVSize; }
  149. LL_INLINE F32 getPixelArea() const { return mPixelArea; }
  150. LL_INLINE S32 getIndexInTex(U32 ch) const
  151. {
  152. return ch < LLRender::NUM_TEXTURE_CHANNELS ? mIndexInTex[ch] : 0;
  153. }
  154. LL_INLINE void setIndexInTex(U32 ch, S32 idx)
  155. {
  156. if (ch < LLRender::NUM_TEXTURE_CHANNELS)
  157. {
  158. mIndexInTex[ch] = idx;
  159. }
  160. }
  161. void renderIndexed(U32 mask = 0);
  162. LL_INLINE const LLTextureEntry* getTextureEntry() const
  163. {
  164. return mTEOffset >= 0 && mVObjp.notNull() ? mVObjp->getTE(mTEOffset)
  165. : NULL;
  166. }
  167. // Returns true when the face texture can be safely included in a render
  168. // batch. This used to be a static (local) can_batch_texture() helper
  169. // function in llvovolume.cpp. HB
  170. bool canBatchTexture() const;
  171. LL_INLINE LLFacePool* getPool() const { return mDrawPoolp; }
  172. LL_INLINE void setPoolType(U32 type) { mPoolType = type; }
  173. LL_INLINE U32 getPoolType() const { return mPoolType; }
  174. // Returns true if this face is in an alpha draw pool.
  175. bool isInAlphaPool() const;
  176. LL_INLINE LLPointer<LLDrawable> getDrawable() const { return mDrawablep; }
  177. LL_INLINE LLViewerObject* getViewerObject() const { return mVObjp; }
  178. LL_INLINE S32 getLOD() const
  179. {
  180. return mVObjp.notNull() ? mVObjp->getLOD() : 0;
  181. }
  182. LL_INLINE S32 getTEOffset() const { return mTEOffset; }
  183. LLViewerTexture* getTexture(U32 ch = LLRender::DIFFUSE_MAP) const;
  184. LL_INLINE void setViewerObject(LLViewerObject* obj) { mVObjp = obj; }
  185. void setPool(LLFacePool* poolp, LLViewerTexture* texp);
  186. LL_INLINE void setPool(LLFacePool* poolp) { mDrawPoolp = poolp; }
  187. void setDrawable(LLDrawable* drawablep);
  188. LL_INLINE void setTEOffset(S32 te_offset) { mTEOffset = te_offset; }
  189. // Override material color
  190. LL_INLINE void setFaceColor(const LLColor4& color)
  191. {
  192. mFaceColor = color;
  193. setState(USE_FACE_COLOR);
  194. }
  195. // Switch back to material color
  196. LL_INLINE void unsetFaceColor() { clearState(USE_FACE_COLOR); }
  197. LL_INLINE const LLColor4& getFaceColor() const { return mFaceColor; }
  198. const LLColor4& getRenderColor() const;
  199. // For volumes
  200. void updateRebuildFlags();
  201. bool canRenderAsMask(); // Logic helper
  202. bool getGeometryVolume(const LLVolume& volume, S32 f,
  203. const LLMatrix4& mat_vert,
  204. const LLMatrix3& mat_normal,
  205. const U16& index_offset,
  206. bool force_rebuild = false);
  207. // For avatar
  208. U16 getGeometryAvatar(LLStrider<LLVector3>& vertices,
  209. LLStrider<LLVector3>& normals,
  210. LLStrider<LLVector2>& texCoords,
  211. LLStrider<F32>& vertex_weights,
  212. LLStrider<LLVector4a>& clothing_weights);
  213. // For volumes, etc.
  214. U16 getGeometry(LLStrider<LLVector3>& vertices,
  215. LLStrider<LLVector3>& normals,
  216. LLStrider<LLVector2>& texCoords,
  217. LLStrider<U16>& indices);
  218. S32 getColors(LLStrider<LLColor4U>& colors);
  219. S32 getIndices(LLStrider<U16>& indices);
  220. void setSize(U32 num_vertices, U32 num_indices = 0, bool align = false);
  221. bool genVolumeBBoxes(const LLVolume& volume, S32 f, const LLMatrix4& mat,
  222. bool global_volume = false);
  223. void init(LLDrawable* drawablep, LLViewerObject* objp);
  224. void destroy();
  225. void update();
  226. // Updates center when xform has changed.
  227. void updateCenterAgent();
  228. void renderSelected(LLViewerTexture* image, const LLColor4& color);
  229. LL_INLINE F32 getKey() const { return mDistance; }
  230. LL_INLINE S32 getReferenceIndex() const { return mReferenceIndex; }
  231. LL_INLINE void setReferenceIndex(S32 index) { mReferenceIndex = index; }
  232. bool verify(const U32* indices_array = NULL) const;
  233. void printDebugInfo() const;
  234. void setGeomIndex(U16 idx);
  235. void setIndicesIndex(U32 idx);
  236. LL_INLINE void setDrawInfo(LLDrawInfo* infop) { mDrawInfo = infop; }
  237. // Return mSkinInfo->mHash or 0 if mSkinInfo is null
  238. U64 getSkinHash() const;
  239. F32 getTextureVirtualSize();
  240. LL_INLINE F32 getImportanceToCamera() const { return mImportanceToCamera; }
  241. void resetVirtualSize();
  242. LL_INLINE void setHasMedia(bool has_media) { mHasMedia = has_media; }
  243. bool hasMedia() const;
  244. LL_INLINE void setMediaAllowed(bool allowed) { mIsMediaAllowed = allowed; }
  245. LL_INLINE bool isMediaAllowed() const { return mIsMediaAllowed; }
  246. // Vertex buffer tracking
  247. void setVertexBuffer(LLVertexBuffer* buffer);
  248. // Sets mVertexBuffer to NULL
  249. void clearVertexBuffer();
  250. LL_INLINE LLVertexBuffer* getVertexBuffer() const { return mVertexBuffer; }
  251. S32 getRiggedIndex(U32 type) const;
  252. static F32 calcImportanceToCamera(F32 to_view_dir, F32 dist);
  253. static F32 adjustPixelArea(F32 importance, F32 pixel_area);
  254. struct CompareDistanceGreater
  255. {
  256. LL_INLINE bool operator()(const LLFace* const& lhs,
  257. const LLFace* const& rhs)
  258. {
  259. // Farthest = first
  260. return !lhs || (rhs && lhs->mDistance > rhs->mDistance);
  261. }
  262. };
  263. struct CompareTexture
  264. {
  265. LL_INLINE bool operator()(const LLFace* const& lhs,
  266. const LLFace* const& rhs)
  267. {
  268. return lhs->getTexture() < rhs->getTexture();
  269. }
  270. };
  271. struct CompareBatchBreaker
  272. {
  273. LL_INLINE bool operator()(const LLFace* const& lhs,
  274. const LLFace* const& rhs)
  275. {
  276. const LLTextureEntry* lte = lhs->getTextureEntry();
  277. const LLTextureEntry* rte = rhs->getTextureEntry();
  278. if (lhs->getTexture() != rhs->getTexture())
  279. {
  280. return lhs->getTexture() < rhs->getTexture();
  281. }
  282. return lte->getBumpShinyFullbright() <
  283. rte->getBumpShinyFullbright();
  284. }
  285. };
  286. struct CompareTextureAndGeomCount
  287. {
  288. LL_INLINE bool operator()(const LLFace* const& lhs,
  289. const LLFace* const& rhs)
  290. {
  291. // Smallest = first
  292. if (lhs->getTexture() == rhs->getTexture())
  293. {
  294. return lhs->getGeomCount() < rhs->getGeomCount();
  295. }
  296. return lhs->getTexture() > rhs->getTexture();
  297. }
  298. };
  299. struct CompareTextureAndLOD
  300. {
  301. LL_INLINE bool operator()(const LLFace* const& lhs,
  302. const LLFace* const& rhs)
  303. {
  304. if (lhs->getTexture() == rhs->getTexture())
  305. {
  306. return lhs->getLOD() < rhs->getLOD();
  307. }
  308. return lhs->getTexture() < rhs->getTexture();
  309. }
  310. };
  311. struct CompareTextureAndTime
  312. {
  313. LL_INLINE bool operator()(const LLFace* const& lhs,
  314. const LLFace* const& rhs)
  315. {
  316. if (lhs->getTexture() == rhs->getTexture())
  317. {
  318. return lhs->mLastUpdateTime < rhs->mLastUpdateTime;
  319. }
  320. return lhs->getTexture() < rhs->getTexture();
  321. }
  322. };
  323. private:
  324. F32 adjustPartialOverlapPixelArea(F32 cos_angle_to_view_dir, F32 radius);
  325. bool calcPixelArea(F32& cos_angle_to_view_dir, F32& radius);
  326. public:
  327. // Aligned member
  328. alignas(16) LLVector4a mExtents[2];
  329. LLVector3 mCenterLocal;
  330. LLVector3 mCenterAgent;
  331. LLVector2 mTexExtents[2];
  332. F32 mDistance;
  333. F32 mLastUpdateTime;
  334. F32 mLastSkinTime;
  335. F32 mLastMoveTime;
  336. LLMatrix4* mTextureMatrix;
  337. LLDrawInfo* mDrawInfo;
  338. LLVOAvatar* mAvatar;
  339. LLPointer<LLMeshSkinInfo> mSkinInfo;
  340. private:
  341. LLPointer<LLVertexBuffer> mVertexBuffer;
  342. LLPointer<LLDrawable> mDrawablep;
  343. LLPointer<LLViewerObject> mVObjp;
  344. LLPointer<LLViewerTexture> mTexture[LLRender::NUM_TEXTURE_CHANNELS];
  345. std::vector<S32> mRiggedIndex;
  346. // Overrides material color if state |= USE_FACE_COLOR
  347. LLColor4 mFaceColor;
  348. LLXformMatrix* mXform;
  349. LLFacePool* mDrawPoolp;
  350. U32 mPoolType;
  351. U32 mState;
  352. U32 mDrawOrderIndex;
  353. F32 mVSize;
  354. F32 mPixelArea;
  355. // Importance factor, in the range [0, 1.0]. 1.0: the most important.
  356. // Based on the distance from the face to the view point and the angle from
  357. // the face center to the view direction.
  358. F32 mImportanceToCamera;
  359. F32 mBoundingSphereRadius;
  360. S32 mTEOffset;
  361. S32 mReferenceIndex;
  362. // Index into draw pool for indices (yeah, I know !)
  363. U32 mIndicesIndex;
  364. U32 mIndicesCount;
  365. // Vertex count for this face
  366. U16 mGeomCount;
  367. // Index into draw pool
  368. U16 mGeomIndex;
  369. S32 mIndexInTex[LLRender::NUM_TEXTURE_CHANNELS];
  370. // Index of texture channel to use for pseudo-atlasing
  371. U8 mTextureIndex;
  372. bool mHasMedia;
  373. bool mIsMediaAllowed;
  374. };
  375. #endif // LL_LLFACE_H