llgltexture.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * @file llgltexture.h
  3. * @brief Object for managing OpenGL textures
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewergpl$
  6. *
  7. * Copyright (c) 2012, 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_GL_TEXTURE_H
  33. #define LL_GL_TEXTURE_H
  34. #include "llgl.h"
  35. #include "llrefcount.h"
  36. #include "llrender.h"
  37. // LL's code implicitely sets texture NO_DELETE when calling dontDiscard()
  38. // or setBoostLevel() with most level values. This is a BOGUS thing to do,
  39. // since it causes many textures that do not deserve/require it to stay forever
  40. // in memory. To reenable this bogus behaviour, set this to 1. HB
  41. #define LL_IMPLICIT_SETNODELETE 0
  42. class LLFontGL;
  43. class LLImageGL;
  44. class LLImageRaw;
  45. class LLUUID;
  46. class LLViewerFetchedTexture;
  47. // This is the parent for the LLViewerTexture class. Via its virtual methods,
  48. // the LLViewerTexture class can be reached from llrender.
  49. // Note: LLGLTexture must now derive from LLThreadSafeRefCount instead of
  50. // LLRefCount because ref() and unref() are used in GL threads. HB
  51. class LLGLTexture : public virtual LLThreadSafeRefCount
  52. {
  53. friend class LLFontGL;
  54. friend class LLTexUnit;
  55. protected:
  56. LOG_CLASS(LLGLTexture);
  57. public:
  58. enum
  59. {
  60. INVALID_DISCARD_LEVEL = 0x7fff
  61. };
  62. enum EBoostLevel : U32
  63. {
  64. BOOST_NONE = 0,
  65. // Equivalent to BOOST_NONE when ALM is on, max discard when off. Not
  66. // used any more by LL's PBR viewer (since it always downloads all
  67. // materials).
  68. BOOST_ALM,
  69. BOOST_AVATAR,
  70. BOOST_CLOUDS,
  71. BOOST_HIGH = 10,
  72. BOOST_SCULPTED, // Has to be high prio to rez fast enough. HB
  73. BOOST_TERRAIN, // Has to be high prio for minimap/low detail
  74. BOOST_SELECTED,
  75. // Textures higher than this need to be downloaded at the required
  76. // resolution without delay.
  77. BOOST_SUPER_HIGH,
  78. // Textures bearing these priorities and set NO_DELETE are never forced
  79. // back to the ACTIVE state by LLImageGL::activateStaleTextures(). HB
  80. BOOST_AVATAR_SELF,
  81. BOOST_HUD,
  82. BOOST_UI, // Automatically set ALWAYS_KEEP & no discard. HB
  83. BOOST_BUMP,
  84. BOOST_MEDIA,
  85. BOOST_PREVIEW,
  86. BOOST_MAP, // Always implicitely set NO_DELETE. HB
  87. BOOST_MAX_LEVEL
  88. };
  89. typedef enum
  90. {
  91. // After the GL image (mImageGLp) has been removed from memory.
  92. DELETED = 0,
  93. // Ready to be removed from memory
  94. DELETION_CANDIDATE,
  95. // Set when not having been used for a certain period (30 seconds).
  96. INACTIVE,
  97. // Just being used, can become inactive if not being used for a certain
  98. // time (10 seconds).
  99. ACTIVE,
  100. // Stays in memory, cannot be removed, unless set forceActive().
  101. NO_DELETE = 99,
  102. // Stays in memory, cannot be removed at all. Only for UI textures. HB
  103. ALWAYS_KEEP = 100
  104. } eState;
  105. protected:
  106. ~LLGLTexture() override;
  107. public:
  108. LLGLTexture(bool usemipmaps = true);
  109. LLGLTexture(const LLImageRaw* raw, bool usemipmaps);
  110. LLGLTexture(U32 width, U32 height, U8 components, bool usemipmaps);
  111. // Logs debug info
  112. virtual void dump();
  113. virtual const LLUUID& getID() const = 0;
  114. virtual void setBoostLevel(U32 level);
  115. LL_INLINE U32 getBoostLevel() { return mBoostLevel; }
  116. LL_INLINE S32 getFullWidth() const { return mFullWidth; }
  117. LL_INLINE S32 getFullHeight() const { return mFullHeight; }
  118. void generateGLTexture();
  119. void destroyGLTexture();
  120. LL_INLINE virtual LLViewerFetchedTexture* asFetched()
  121. {
  122. return NULL;
  123. }
  124. LL_INLINE void setActive()
  125. {
  126. if (mTextureState < NO_DELETE)
  127. {
  128. mTextureState = ACTIVE;
  129. }
  130. }
  131. LL_INLINE void forceActive()
  132. {
  133. if (mTextureState != ALWAYS_KEEP)
  134. {
  135. mTextureState = ACTIVE;
  136. }
  137. }
  138. LL_INLINE void setNoDelete()
  139. {
  140. if (mTextureState != ALWAYS_KEEP)
  141. {
  142. mTextureState = NO_DELETE;
  143. }
  144. }
  145. LL_INLINE bool isNoDelete() const
  146. {
  147. return mTextureState == NO_DELETE;
  148. }
  149. LL_INLINE void dontDiscard()
  150. {
  151. mDontDiscard = true;
  152. #if LL_IMPLICIT_SETNODELETE
  153. setNoDelete();
  154. #endif
  155. }
  156. LL_INLINE bool getDontDiscard() const { return mDontDiscard; }
  157. LL_INLINE void setMegaTexture(bool b = true) { mIsMegaTexture = b; }
  158. //-------------------------------------------------------------------------
  159. // Methods to access LLImageGL
  160. //-------------------------------------------------------------------------
  161. bool hasGLTexture() const;
  162. U32 getTexName() const;
  163. bool createGLTexture();
  164. // Creates a GL texture from a raw image. With:
  165. // - discard_level: mip level, 0 for highest resolution mip
  166. // - imageraw: the image to copy from
  167. // - usename: explicit GL name override
  168. // - to_create: false to force GL texture to not be created
  169. // - defer_copy: true to allocate GL texture but NOT initialize with
  170. // imageraw data
  171. // - tex_name: if not null, will be set to the GL name of the texture
  172. // created
  173. bool createGLTexture(S32 discard_level, const LLImageRaw* imageraw,
  174. S32 usename = 0, bool to_create = true,
  175. bool defer_copy = false, U32* tex_name = NULL);
  176. void setFilteringOption(LLTexUnit::eTextureFilterOptions option);
  177. void setExplicitFormat(S32 internal_format, U32 primary_format,
  178. U32 type_format = 0, bool swap_bytes = false);
  179. void setAddressMode(LLTexUnit::eTextureAddressMode mode);
  180. bool setSubImage(const LLImageRaw* imageraw, S32 x_pos, S32 y_pos,
  181. S32 width, S32 height, U32 use_name = 0);
  182. bool setSubImage(const U8* datap, S32 data_width, S32 data_height,
  183. S32 x_pos, S32 y_pos, S32 width, S32 height,
  184. U32 use_name = 0);
  185. void setGLTextureCreated (bool initialized);
  186. // For forcing with externally created textures only:
  187. #if 0 // Not used
  188. void setTexName(U32 name);
  189. void setTarget(U32 target, LLTexUnit::eTextureType bind_target);
  190. #endif
  191. LLTexUnit::eTextureAddressMode getAddressMode() const;
  192. S32 getMaxDiscardLevel() const;
  193. S32 getDiscardLevel() const;
  194. S8 getComponents() const;
  195. bool getBoundRecently() const;
  196. S32 getTextureMemory() const;
  197. U32 getPrimaryFormat() const;
  198. bool getIsAlphaMask() const;
  199. LLTexUnit::eTextureType getTarget() const;
  200. bool getMask(const LLVector2 &tc);
  201. F32 getTimePassedSinceLastBound();
  202. bool isJustBound()const;
  203. void forceUpdateBindStats() const;
  204. LL_INLINE S32 getTextureState() const { return mTextureState; }
  205. S32 getMinDiscardLevel() const;
  206. //-------------------------------------------------------------------------
  207. // Virtual interface to access LLViewerTexture
  208. //-------------------------------------------------------------------------
  209. virtual S32 getWidth(S32 discard_level = -1) const;
  210. virtual S32 getHeight(S32 discard_level = -1) const;
  211. virtual S8 getType() const = 0;
  212. virtual void setKnownDrawSize(S32 width, S32 height) = 0;
  213. virtual bool bindDefaultImage(S32 stage = 0) = 0;
  214. virtual void forceImmediateUpdate() = 0;
  215. LLImageGL* getGLImage() const;
  216. private:
  217. void cleanup();
  218. void init();
  219. protected:
  220. void setTexelsPerImage();
  221. protected:
  222. // Note: the first member variable is 32 bits in order to align on 64 bits
  223. // for the next variable, counting the (assumed/normally, for lock-free
  224. // std::atomic implementations) 32 bits atomic counter from
  225. // LLThreadSafeRefCount. HB
  226. S32 mTextureState;
  227. LLPointer<LLImageGL> mImageGLp;
  228. U32 mBoostLevel; // enum describing priority level
  229. S32 mFullWidth;
  230. S32 mFullHeight;
  231. S32 mTexelsPerImage;
  232. S8 mComponents;
  233. bool mUseMipMaps;
  234. // Set to true to keep full resolution version of this image (for UI, etc)
  235. bool mDontDiscard;
  236. // Used to allow overriding the download render limit for textures that do
  237. // deserve being larger than 1024x1024. Typically only for PBR terrain
  238. // textures, for now. HB
  239. bool mIsMegaTexture;
  240. mutable bool mNeedsGLTexture;
  241. };
  242. extern S32 gMaxImageSizeDefault;
  243. #endif // LL_GL_TEXTURE_H