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