PageRenderTime 89ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llrender/llimagegl.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 307 lines | 184 code | 63 blank | 60 comment | 1 complexity | 3a92e67876da901df61adfab4384d449 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llimagegl.h
  3. * @brief Object for managing images and their textures
  4. *
  5. * $LicenseInfo:firstyear=2001&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. #ifndef LL_LLIMAGEGL_H
  27. #define LL_LLIMAGEGL_H
  28. #include "llimage.h"
  29. #include "llgltypes.h"
  30. #include "llpointer.h"
  31. #include "llrefcount.h"
  32. #include "v2math.h"
  33. #include "llrender.h"
  34. class LLTextureAtlas ;
  35. #define BYTES_TO_MEGA_BYTES(x) ((x) >> 20)
  36. #define MEGA_BYTES_TO_BYTES(x) ((x) << 20)
  37. //============================================================================
  38. class LLImageGL : public LLRefCount
  39. {
  40. friend class LLTexUnit;
  41. public:
  42. static std::list<U32> sDeadTextureList;
  43. static void deleteDeadTextures();
  44. // Size calculation
  45. static S32 dataFormatBits(S32 dataformat);
  46. static S32 dataFormatBytes(S32 dataformat, S32 width, S32 height);
  47. static S32 dataFormatComponents(S32 dataformat);
  48. BOOL updateBindStats(S32 tex_mem) const ;
  49. F32 getTimePassedSinceLastBound();
  50. void forceUpdateBindStats(void) const;
  51. // needs to be called every frame
  52. static void updateStats(F32 current_time);
  53. // Save off / restore GL textures
  54. static void destroyGL(BOOL save_state = TRUE);
  55. static void restoreGL();
  56. static void dirtyTexOptions();
  57. // Sometimes called externally for textures not using LLImageGL (should go away...)
  58. static S32 updateBoundTexMem(const S32 mem, const S32 ncomponents, S32 category) ;
  59. static bool checkSize(S32 width, S32 height);
  60. //for server side use only.
  61. // Not currently necessary for LLImageGL, but required in some derived classes,
  62. // so include for compatability
  63. static BOOL create(LLPointer<LLImageGL>& dest, BOOL usemipmaps = TRUE);
  64. static BOOL create(LLPointer<LLImageGL>& dest, U32 width, U32 height, U8 components, BOOL usemipmaps = TRUE);
  65. static BOOL create(LLPointer<LLImageGL>& dest, const LLImageRaw* imageraw, BOOL usemipmaps = TRUE);
  66. public:
  67. LLImageGL(BOOL usemipmaps = TRUE);
  68. LLImageGL(U32 width, U32 height, U8 components, BOOL usemipmaps = TRUE);
  69. LLImageGL(const LLImageRaw* imageraw, BOOL usemipmaps = TRUE);
  70. protected:
  71. virtual ~LLImageGL();
  72. void analyzeAlpha(const void* data_in, U32 w, U32 h);
  73. void calcAlphaChannelOffsetAndStride();
  74. public:
  75. virtual void dump(); // debugging info to llinfos
  76. void setSize(S32 width, S32 height, S32 ncomponents);
  77. void setComponents(S32 ncomponents) { mComponents = (S8)ncomponents ;}
  78. // These 3 functions currently wrap glGenTextures(), glDeleteTextures(), and glTexImage2D()
  79. // for tracking purposes and will be deprecated in the future
  80. static void generateTextures(S32 numTextures, U32 *textures);
  81. static void deleteTextures(S32 numTextures, U32 *textures, bool immediate = false);
  82. static void setManualImage(U32 target, S32 miplevel, S32 intformat, S32 width, S32 height, U32 pixformat, U32 pixtype, const void *pixels);
  83. BOOL createGLTexture() ;
  84. BOOL createGLTexture(S32 discard_level, const LLImageRaw* imageraw, S32 usename = 0, BOOL to_create = TRUE,
  85. S32 category = sMaxCatagories - 1);
  86. BOOL createGLTexture(S32 discard_level, const U8* data, BOOL data_hasmips = FALSE, S32 usename = 0);
  87. void setImage(const LLImageRaw* imageraw);
  88. void setImage(const U8* data_in, BOOL data_hasmips = FALSE);
  89. BOOL setSubImage(const LLImageRaw* imageraw, S32 x_pos, S32 y_pos, S32 width, S32 height, BOOL force_fast_update = FALSE);
  90. BOOL setSubImage(const U8* datap, S32 data_width, S32 data_height, S32 x_pos, S32 y_pos, S32 width, S32 height, BOOL force_fast_update = FALSE);
  91. BOOL setSubImageFromFrameBuffer(S32 fb_x, S32 fb_y, S32 x_pos, S32 y_pos, S32 width, S32 height);
  92. // Read back a raw image for this discard level, if it exists
  93. BOOL readBackRaw(S32 discard_level, LLImageRaw* imageraw, bool compressed_ok) const;
  94. void destroyGLTexture();
  95. void setExplicitFormat(LLGLint internal_format, LLGLenum primary_format, LLGLenum type_format = 0, BOOL swap_bytes = FALSE);
  96. void setComponents(S8 ncomponents) { mComponents = ncomponents; }
  97. S32 getDiscardLevel() const { return mCurrentDiscardLevel; }
  98. S32 getMaxDiscardLevel() const { return mMaxDiscardLevel; }
  99. S32 getCurrentWidth() const { return mWidth ;}
  100. S32 getCurrentHeight() const { return mHeight ;}
  101. S32 getWidth(S32 discard_level = -1) const;
  102. S32 getHeight(S32 discard_level = -1) const;
  103. U8 getComponents() const { return mComponents; }
  104. S32 getBytes(S32 discard_level = -1) const;
  105. S32 getMipBytes(S32 discard_level = -1) const;
  106. BOOL getBoundRecently() const;
  107. BOOL isJustBound() const;
  108. LLGLenum getPrimaryFormat() const { return mFormatPrimary; }
  109. LLGLenum getFormatType() const { return mFormatType; }
  110. BOOL getHasGLTexture() const { return mTexName != 0; }
  111. LLGLuint getTexName() const { return mTexName; }
  112. BOOL getIsAlphaMask() const { return mIsMask; }
  113. BOOL getIsResident(BOOL test_now = FALSE); // not const
  114. void setTarget(const LLGLenum target, const LLTexUnit::eTextureType bind_target);
  115. LLTexUnit::eTextureType getTarget(void) const { return mBindTarget; }
  116. bool isGLTextureCreated(void) const { return mGLTextureCreated ; }
  117. void setGLTextureCreated (bool initialized) { mGLTextureCreated = initialized; }
  118. BOOL getUseMipMaps() const { return mUseMipMaps; }
  119. void setUseMipMaps(BOOL usemips) { mUseMipMaps = usemips; }
  120. void updatePickMask(S32 width, S32 height, const U8* data_in);
  121. BOOL getMask(const LLVector2 &tc);
  122. void checkTexSize(bool forced = false) const ;
  123. // Sets the addressing mode used to sample the texture
  124. // (such as wrapping, mirrored wrapping, and clamp)
  125. // Note: this actually gets set the next time the texture is bound.
  126. void setAddressMode(LLTexUnit::eTextureAddressMode mode);
  127. LLTexUnit::eTextureAddressMode getAddressMode(void) const { return mAddressMode; }
  128. // Sets the filtering options used to sample the texture
  129. // (such as point sampling, bilinear interpolation, mipmapping, and anisotropic filtering)
  130. // Note: this actually gets set the next time the texture is bound.
  131. void setFilteringOption(LLTexUnit::eTextureFilterOptions option);
  132. LLTexUnit::eTextureFilterOptions getFilteringOption(void) const { return mFilterOption; }
  133. LLGLenum getTexTarget()const { return mTarget ;}
  134. S8 getDiscardLevelInAtlas()const {return mDiscardLevelInAtlas;}
  135. U32 getTexelsInAtlas()const { return mTexelsInAtlas ;}
  136. U32 getTexelsInGLTexture()const {return mTexelsInGLTexture;}
  137. void init(BOOL usemipmaps);
  138. virtual void cleanup(); // Clean up the LLImageGL so it can be reinitialized. Be careful when using this in derived class destructors
  139. void setNeedsAlphaAndPickMask(BOOL need_mask);
  140. BOOL preAddToAtlas(S32 discard_level, const LLImageRaw* raw_image);
  141. void postAddToAtlas() ;
  142. public:
  143. // Various GL/Rendering options
  144. S32 mTextureMemory;
  145. mutable F32 mLastBindTime; // last time this was bound, by discard level
  146. private:
  147. LLPointer<LLImageRaw> mSaveData; // used for destroyGL/restoreGL
  148. U8* mPickMask; //downsampled bitmap approximation of alpha channel. NULL if no alpha channel
  149. U16 mPickMaskWidth;
  150. U16 mPickMaskHeight;
  151. S8 mUseMipMaps;
  152. S8 mHasExplicitFormat; // If false (default), GL format is f(mComponents)
  153. S8 mAutoGenMips;
  154. BOOL mIsMask;
  155. BOOL mNeedsAlphaAndPickMask;
  156. S8 mAlphaStride ;
  157. S8 mAlphaOffset ;
  158. bool mGLTextureCreated ;
  159. LLGLuint mTexName;
  160. U16 mWidth;
  161. U16 mHeight;
  162. S8 mCurrentDiscardLevel;
  163. S8 mDiscardLevelInAtlas;
  164. U32 mTexelsInAtlas ;
  165. U32 mTexelsInGLTexture;
  166. protected:
  167. LLGLenum mTarget; // Normally GL_TEXTURE2D, sometimes something else (ex. cube maps)
  168. LLTexUnit::eTextureType mBindTarget; // Normally TT_TEXTURE, sometimes something else (ex. cube maps)
  169. bool mHasMipMaps;
  170. LLGLboolean mIsResident;
  171. S8 mComponents;
  172. S8 mMaxDiscardLevel;
  173. bool mTexOptionsDirty;
  174. LLTexUnit::eTextureAddressMode mAddressMode; // Defaults to TAM_WRAP
  175. LLTexUnit::eTextureFilterOptions mFilterOption; // Defaults to TFO_ANISOTROPIC
  176. LLGLint mFormatInternal; // = GL internalformat
  177. LLGLenum mFormatPrimary; // = GL format (pixel data format)
  178. LLGLenum mFormatType;
  179. BOOL mFormatSwapBytes;// if true, use glPixelStorei(GL_UNPACK_SWAP_BYTES, 1)
  180. // STATICS
  181. public:
  182. static std::set<LLImageGL*> sImageList;
  183. static S32 sCount;
  184. static F32 sLastFrameTime;
  185. static LLGLuint sCurrentBoundTextures[MAX_GL_TEXTURE_UNITS]; // Currently bound texture ID
  186. // Global memory statistics
  187. static S32 sGlobalTextureMemoryInBytes; // Tracks main memory texmem
  188. static S32 sBoundTextureMemoryInBytes; // Tracks bound texmem for last completed frame
  189. static S32 sCurBoundTextureMemory; // Tracks bound texmem for current frame
  190. static U32 sBindCount; // Tracks number of texture binds for current frame
  191. static U32 sUniqueCount; // Tracks number of unique texture binds for current frame
  192. static BOOL sGlobalUseAnisotropic;
  193. static LLImageGL* sDefaultGLTexture ;
  194. static BOOL sAutomatedTest;
  195. #if DEBUG_MISS
  196. BOOL mMissed; // Missed on last bind?
  197. BOOL getMissed() const { return mMissed; };
  198. #else
  199. BOOL getMissed() const { return FALSE; };
  200. #endif
  201. public:
  202. static void initClass(S32 num_catagories) ;
  203. static void cleanupClass() ;
  204. private:
  205. static S32 sMaxCatagories ;
  206. //the flag to allow to call readBackRaw(...).
  207. //can be removed if we do not use that function at all.
  208. static BOOL sAllowReadBackRaw ;
  209. //
  210. //****************************************************************************************************
  211. //The below for texture auditing use only
  212. //****************************************************************************************************
  213. private:
  214. S32 mCategory ;
  215. public:
  216. void setCategory(S32 category) ;
  217. S32 getCategory()const {return mCategory ;}
  218. //for debug use: show texture size distribution
  219. //----------------------------------------
  220. static LLPointer<LLImageGL> sHighlightTexturep; //default texture to replace normal textures
  221. static std::vector<S32> sTextureLoadedCounter ;
  222. static std::vector<S32> sTextureBoundCounter ;
  223. static std::vector<S32> sTextureCurBoundCounter ;
  224. static S32 sCurTexSizeBar ;
  225. static S32 sCurTexPickSize ;
  226. static void setHighlightTexture(S32 category) ;
  227. static S32 getTextureCounterIndex(U32 val) ;
  228. static void incTextureCounter(U32 val, S32 ncomponents, S32 category) ;
  229. static void decTextureCounter(U32 val, S32 ncomponents, S32 category) ;
  230. static void setCurTexSizebar(S32 index, BOOL set_pick_size = TRUE) ;
  231. static void resetCurTexSizebar();
  232. //----------------------------------------
  233. //for debug use: show texture category distribution
  234. //----------------------------------------
  235. static std::vector<S32> sTextureMemByCategory;
  236. static std::vector<S32> sTextureMemByCategoryBound ;
  237. static std::vector<S32> sTextureCurMemByCategoryBound ;
  238. //----------------------------------------
  239. //****************************************************************************************************
  240. //End of definitions for texture auditing use only
  241. //****************************************************************************************************
  242. };
  243. extern BOOL gAuditTexture;
  244. #endif // LL_LLIMAGEGL_H