PageRenderTime 36ms CodeModel.GetById 3ms app.highlight 27ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llrender/llrender.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 480 lines | 337 code | 82 blank | 61 comment | 0 complexity | 3224743ebbbe22b3a44d55c5a1144949 MD5 | raw file
  1/** 
  2 * @file llrender.h
  3 * @brief LLRender definition
  4 *
  5 *	This class acts as a wrapper for OpenGL calls.
  6 *	The goal of this class is to minimize the number of api calls due to legacy rendering
  7 *	code, to define an interface for a multiple rendering API abstraction of the UI
  8 *	rendering, and to abstract out direct rendering calls in a way that is cleaner and easier to maintain.
  9 *
 10 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 11 * Second Life Viewer Source Code
 12 * Copyright (C) 2010, Linden Research, Inc.
 13 * 
 14 * This library is free software; you can redistribute it and/or
 15 * modify it under the terms of the GNU Lesser General Public
 16 * License as published by the Free Software Foundation;
 17 * version 2.1 of the License only.
 18 * 
 19 * This library is distributed in the hope that it will be useful,
 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 22 * Lesser General Public License for more details.
 23 * 
 24 * You should have received a copy of the GNU Lesser General Public
 25 * License along with this library; if not, write to the Free Software
 26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 27 * 
 28 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 29 * $/LicenseInfo$
 30 */
 31
 32#ifndef LL_LLGLRENDER_H
 33#define LL_LLGLRENDER_H
 34
 35//#include "linden_common.h"
 36
 37#include "v2math.h"
 38#include "v3math.h"
 39#include "v4coloru.h"
 40#include "v4math.h"
 41#include "llstrider.h"
 42#include "llpointer.h"
 43#include "llglheaders.h"
 44#include "llmatrix4a.h"
 45#include "glh/glh_linear.h"
 46
 47class LLVertexBuffer;
 48class LLCubeMap;
 49class LLImageGL;
 50class LLRenderTarget;
 51class LLTexture ;
 52
 53#define LL_MATRIX_STACK_DEPTH 32
 54
 55class LLTexUnit
 56{
 57	friend class LLRender;
 58public:
 59	static U32 sWhiteTexture;
 60
 61	typedef enum
 62	{
 63		TT_TEXTURE = 0,			// Standard 2D Texture
 64		TT_RECT_TEXTURE,	// Non power of 2 texture
 65		TT_CUBE_MAP,		// 6-sided cube map texture
 66		TT_MULTISAMPLE_TEXTURE, // see GL_ARB_texture_multisample
 67		TT_NONE 		// No texture type is currently enabled
 68	} eTextureType;
 69
 70	typedef enum
 71	{
 72		TAM_WRAP = 0,			// Standard 2D Texture
 73		TAM_MIRROR,				// Non power of 2 texture
 74		TAM_CLAMP 				// No texture type is currently enabled
 75	} eTextureAddressMode;
 76
 77	typedef enum
 78	{	// Note: If mipmapping or anisotropic are not enabled or supported it should fall back gracefully
 79		TFO_POINT = 0,			// Equal to: min=point, mag=point, mip=none.
 80		TFO_BILINEAR,			// Equal to: min=linear, mag=linear, mip=point.
 81		TFO_TRILINEAR,			// Equal to: min=linear, mag=linear, mip=linear.
 82		TFO_ANISOTROPIC			// Equal to: min=anisotropic, max=anisotropic, mip=linear.
 83	} eTextureFilterOptions;
 84
 85	typedef enum 
 86	{
 87		TB_REPLACE = 0,
 88		TB_ADD,
 89		TB_MULT,
 90		TB_MULT_X2,
 91		TB_ALPHA_BLEND,
 92		TB_COMBINE			// Doesn't need to be set directly, setTexture___Blend() set TB_COMBINE automatically
 93	} eTextureBlendType;
 94
 95	typedef enum 
 96	{
 97		TBO_REPLACE = 0,			// Use Source 1
 98		TBO_MULT,					// Multiply: ( Source1 * Source2 )
 99		TBO_MULT_X2,				// Multiply then scale by 2:  ( 2.0 * ( Source1 * Source2 ) )
100		TBO_MULT_X4,				// Multiply then scale by 4:  ( 4.0 * ( Source1 * Source2 ) )
101		TBO_ADD,					// Add: ( Source1 + Source2 )
102		TBO_ADD_SIGNED,				// Add then subtract 0.5: ( ( Source1 + Source2 ) - 0.5 )
103		TBO_SUBTRACT,				// Subtract Source2 from Source1: ( Source1 - Source2 )
104		TBO_LERP_VERT_ALPHA,		// Interpolate based on Vertex Alpha (VA): ( Source1 * VA + Source2 * (1-VA) )
105		TBO_LERP_TEX_ALPHA,			// Interpolate based on Texture Alpha (TA): ( Source1 * TA + Source2 * (1-TA) )
106		TBO_LERP_PREV_ALPHA,		// Interpolate based on Previous Alpha (PA): ( Source1 * PA + Source2 * (1-PA) )
107		TBO_LERP_CONST_ALPHA,		// Interpolate based on Const Alpha (CA): ( Source1 * CA + Source2 * (1-CA) )
108		TBO_LERP_VERT_COLOR			// Interpolate based on Vertex Col (VC): ( Source1 * VC + Source2 * (1-VC) )
109										// *Note* TBO_LERP_VERTEX_COLOR only works with setTextureColorBlend(),
110										// and falls back to TBO_LERP_VERTEX_ALPHA for setTextureAlphaBlend().
111	} eTextureBlendOp;
112
113	typedef enum 
114	{
115		TBS_PREV_COLOR = 0,			// Color from the previous texture stage
116		TBS_PREV_ALPHA,
117		TBS_ONE_MINUS_PREV_COLOR,
118		TBS_ONE_MINUS_PREV_ALPHA,
119		TBS_TEX_COLOR,				// Color from the texture bound to this stage
120		TBS_TEX_ALPHA,
121		TBS_ONE_MINUS_TEX_COLOR,
122		TBS_ONE_MINUS_TEX_ALPHA,
123		TBS_VERT_COLOR,				// The vertex color currently set
124		TBS_VERT_ALPHA,
125		TBS_ONE_MINUS_VERT_COLOR,
126		TBS_ONE_MINUS_VERT_ALPHA,
127		TBS_CONST_COLOR,			// The constant color value currently set
128		TBS_CONST_ALPHA,
129		TBS_ONE_MINUS_CONST_COLOR,
130		TBS_ONE_MINUS_CONST_ALPHA
131	} eTextureBlendSrc;
132
133	LLTexUnit(S32 index);
134
135	// Refreshes renderer state of the texture unit to the cached values
136	// Needed when the render context has changed and invalidated the current state
137	void refreshState(void);
138
139	// returns the index of this texture unit
140	S32 getIndex(void) const { return mIndex; }
141
142	// Sets this tex unit to be the currently active one
143	void activate(void); 
144
145	// Enables this texture unit for the given texture type 
146	// (automatically disables any previously enabled texture type)
147	void enable(eTextureType type); 
148
149	// Disables the current texture unit
150	void disable(void);	
151	
152	// Binds the LLImageGL to this texture unit 
153	// (automatically enables the unit for the LLImageGL's texture type)
154	bool bind(LLImageGL* texture, bool for_rendering = false, bool forceBind = false);
155	bool bind(LLTexture* texture, bool for_rendering = false, bool forceBind = false);
156
157	// Binds a cubemap to this texture unit 
158	// (automatically enables the texture unit for cubemaps)
159	bool bind(LLCubeMap* cubeMap);
160
161	// Binds a render target to this texture unit 
162	// (automatically enables the texture unit for the RT's texture type)
163	bool bind(LLRenderTarget * renderTarget, bool bindDepth = false);
164
165	// Manually binds a texture to the texture unit 
166	// (automatically enables the tex unit for the given texture type)
167	bool bindManual(eTextureType type, U32 texture, bool hasMips = false);
168	
169	// Unbinds the currently bound texture of the given type 
170	// (only if there's a texture of the given type currently bound)
171	void unbind(eTextureType type);
172
173	// Sets the addressing mode used to sample the texture
174	// Warning: this stays set for the bound texture forever, 
175	// make sure you want to permanently change the address mode  for the bound texture.
176	void setTextureAddressMode(eTextureAddressMode mode);
177
178	// Sets the filtering options used to sample the texture
179	// Warning: this stays set for the bound texture forever, 
180	// make sure you want to permanently change the filtering for the bound texture.
181	void setTextureFilteringOption(LLTexUnit::eTextureFilterOptions option);
182
183	void setTextureBlendType(eTextureBlendType type);
184
185	inline void setTextureColorBlend(eTextureBlendOp op, eTextureBlendSrc src1, eTextureBlendSrc src2 = TBS_PREV_COLOR)
186	{ setTextureCombiner(op, src1, src2, false); }
187
188	// NOTE: If *_COLOR enums are passed to src1 or src2, the corresponding *_ALPHA enum will be used instead.
189	inline void setTextureAlphaBlend(eTextureBlendOp op, eTextureBlendSrc src1, eTextureBlendSrc src2 = TBS_PREV_ALPHA)
190	{ setTextureCombiner(op, src1, src2, true); }
191
192	static U32 getInternalType(eTextureType type);
193
194	U32 getCurrTexture(void) { return mCurrTexture; }
195
196	eTextureType getCurrType(void) { return mCurrTexType; }
197
198	void setHasMipMaps(bool hasMips) { mHasMipMaps = hasMips; }
199
200protected:
201	S32					mIndex;
202	U32					mCurrTexture;
203	eTextureType		mCurrTexType;
204	eTextureBlendType	mCurrBlendType;
205	eTextureBlendOp		mCurrColorOp;
206	eTextureBlendSrc	mCurrColorSrc1;
207	eTextureBlendSrc	mCurrColorSrc2;
208	eTextureBlendOp		mCurrAlphaOp;
209	eTextureBlendSrc	mCurrAlphaSrc1;
210	eTextureBlendSrc	mCurrAlphaSrc2;
211	S32					mCurrColorScale;
212	S32					mCurrAlphaScale;
213	bool				mHasMipMaps;
214	
215	void debugTextureUnit(void);
216	void setColorScale(S32 scale);
217	void setAlphaScale(S32 scale);
218	GLint getTextureSource(eTextureBlendSrc src);
219	GLint getTextureSourceType(eTextureBlendSrc src, bool isAlpha = false);
220	void setTextureCombiner(eTextureBlendOp op, eTextureBlendSrc src1, eTextureBlendSrc src2, bool isAlpha = false);
221};
222
223class LLLightState
224{
225public:
226	LLLightState(S32 index);
227
228	void enable();
229	void disable();
230	void setDiffuse(const LLColor4& diffuse);
231	void setAmbient(const LLColor4& ambient);
232	void setSpecular(const LLColor4& specular);
233	void setPosition(const LLVector4& position);
234	void setConstantAttenuation(const F32& atten);
235	void setLinearAttenuation(const F32& atten);
236	void setQuadraticAttenuation(const F32& atten);
237	void setSpotExponent(const F32& exponent);
238	void setSpotCutoff(const F32& cutoff);
239	void setSpotDirection(const LLVector3& direction);
240
241protected:
242	friend class LLRender;
243
244	S32 mIndex;
245	bool mEnabled;
246	LLColor4 mDiffuse;
247	LLColor4 mAmbient;
248	LLColor4 mSpecular;
249	LLVector4 mPosition;
250	LLVector3 mSpotDirection;
251
252	F32 mConstantAtten;
253	F32 mLinearAtten;
254	F32 mQuadraticAtten;
255
256	F32 mSpotExponent;
257	F32 mSpotCutoff;
258};
259
260class LLRender
261{
262	friend class LLTexUnit;
263public:
264
265	typedef enum {
266		TRIANGLES = 0,
267		TRIANGLE_STRIP,
268		TRIANGLE_FAN,
269		POINTS,
270		LINES,
271		LINE_STRIP,
272		QUADS,
273		LINE_LOOP,
274		NUM_MODES
275	} eGeomModes;
276
277	typedef enum 
278	{
279		CF_NEVER = 0,
280		CF_ALWAYS,
281		CF_LESS,
282		CF_LESS_EQUAL,
283		CF_EQUAL,
284		CF_NOT_EQUAL,
285		CF_GREATER_EQUAL,
286		CF_GREATER,
287		CF_DEFAULT
288	}  eCompareFunc;
289
290	typedef enum 
291	{
292		BT_ALPHA = 0,
293		BT_ADD,
294		BT_ADD_WITH_ALPHA,	// Additive blend modulated by the fragment's alpha.
295		BT_MULT,
296		BT_MULT_ALPHA,
297		BT_MULT_X2,
298		BT_REPLACE
299	} eBlendType;
300
301	typedef enum 
302	{
303		BF_ONE = 0,
304		BF_ZERO,
305		BF_DEST_COLOR,
306		BF_SOURCE_COLOR,
307		BF_ONE_MINUS_DEST_COLOR,
308		BF_ONE_MINUS_SOURCE_COLOR,
309		BF_DEST_ALPHA,
310		BF_SOURCE_ALPHA,
311		BF_ONE_MINUS_DEST_ALPHA,
312		BF_ONE_MINUS_SOURCE_ALPHA,
313
314		BF_UNDEF
315	} eBlendFactor;
316
317	typedef enum
318	{
319		MM_MODELVIEW = 0,
320		MM_PROJECTION,
321		MM_TEXTURE0,
322		MM_TEXTURE1,
323		MM_TEXTURE2,
324		MM_TEXTURE3,
325		NUM_MATRIX_MODES,
326		MM_TEXTURE
327	} eMatrixMode;
328
329	LLRender();
330	~LLRender();
331	void init() ;
332	void shutdown();
333	
334	// Refreshes renderer state to the cached values
335	// Needed when the render context has changed and invalidated the current state
336	void refreshState(void);
337
338	void translatef(const GLfloat& x, const GLfloat& y, const GLfloat& z);
339	void scalef(const GLfloat& x, const GLfloat& y, const GLfloat& z);
340	void rotatef(const GLfloat& a, const GLfloat& x, const GLfloat& y, const GLfloat& z);
341	void ortho(F32 left, F32 right, F32 bottom, F32 top, F32 zNear, F32 zFar);
342
343	void pushMatrix();
344	void popMatrix();
345	void loadMatrix(const GLfloat* m);
346	void loadIdentity();
347	void multMatrix(const GLfloat* m);
348	void matrixMode(U32 mode);	
349
350	const glh::matrix4f& getModelviewMatrix();
351	const glh::matrix4f& getProjectionMatrix();
352
353	void syncMatrices();
354	void syncLightState();
355
356	void translateUI(F32 x, F32 y, F32 z);
357	void scaleUI(F32 x, F32 y, F32 z);
358	void pushUIMatrix();
359	void popUIMatrix();
360	void loadUIIdentity();
361	LLVector3 getUITranslation();
362	LLVector3 getUIScale();
363
364	void flush();
365
366	void begin(const GLuint& mode);
367	void end();
368	void vertex2i(const GLint& x, const GLint& y);
369	void vertex2f(const GLfloat& x, const GLfloat& y);
370	void vertex3f(const GLfloat& x, const GLfloat& y, const GLfloat& z);
371	void vertex2fv(const GLfloat* v);
372	void vertex3fv(const GLfloat* v);
373	
374	void texCoord2i(const GLint& x, const GLint& y);
375	void texCoord2f(const GLfloat& x, const GLfloat& y);
376	void texCoord2fv(const GLfloat* tc);
377
378	void color4ub(const GLubyte& r, const GLubyte& g, const GLubyte& b, const GLubyte& a);
379	void color4f(const GLfloat& r, const GLfloat& g, const GLfloat& b, const GLfloat& a);
380	void color4fv(const GLfloat* c);
381	void color3f(const GLfloat& r, const GLfloat& g, const GLfloat& b);
382	void color3fv(const GLfloat* c);
383	void color4ubv(const GLubyte* c);
384
385	void diffuseColor3f(F32 r, F32 g, F32 b);
386	void diffuseColor3fv(const F32* c);
387	void diffuseColor4f(F32 r, F32 g, F32 b, F32 a);
388	void diffuseColor4fv(const F32* c);
389	void diffuseColor4ubv(const U8* c);
390
391	void vertexBatchPreTransformed(LLVector3* verts, S32 vert_count);
392	void vertexBatchPreTransformed(LLVector3* verts, LLVector2* uvs, S32 vert_count);
393	void vertexBatchPreTransformed(LLVector3* verts, LLVector2* uvs, LLColor4U*, S32 vert_count);
394
395	void setColorMask(bool writeColor, bool writeAlpha);
396	void setColorMask(bool writeColorR, bool writeColorG, bool writeColorB, bool writeAlpha);
397	void setSceneBlendType(eBlendType type);
398
399	void setAlphaRejectSettings(eCompareFunc func, F32 value = 0.01f);
400
401	// applies blend func to both color and alpha
402	void blendFunc(eBlendFactor sfactor, eBlendFactor dfactor);
403	// applies separate blend functions to color and alpha
404	void blendFunc(eBlendFactor color_sfactor, eBlendFactor color_dfactor,
405		       eBlendFactor alpha_sfactor, eBlendFactor alpha_dfactor);
406
407	LLLightState* getLight(U32 index);
408	void setAmbientLightColor(const LLColor4& color);
409	
410	LLTexUnit* getTexUnit(U32 index);
411
412	U32	getCurrentTexUnitIndex(void) const { return mCurrTextureUnitIndex; }
413
414	bool verifyTexUnitActive(U32 unitToVerify);
415
416	void debugTexUnits(void);
417
418	void clearErrors();
419
420	struct Vertex
421	{
422		GLfloat v[3];
423		GLubyte c[4];
424		GLfloat uv[2];
425	};
426
427public:
428	static U32 sUICalls;
429	static U32 sUIVerts;
430	static bool sGLCoreProfile;
431	
432private:
433	friend class LLLightState;
434
435	U32 mMatrixMode;
436	U32 mMatIdx[NUM_MATRIX_MODES];
437	U32 mMatHash[NUM_MATRIX_MODES];
438	glh::matrix4f mMatrix[NUM_MATRIX_MODES][LL_MATRIX_STACK_DEPTH];
439	U32 mCurMatHash[NUM_MATRIX_MODES];
440	U32 mLightHash;
441	LLColor4 mAmbientLightColor;
442	
443	bool			mDirty;
444	U32				mQuadCycle;
445	U32				mCount;
446	U32				mMode;
447	U32				mCurrTextureUnitIndex;
448	bool				mCurrColorMask[4];
449	eCompareFunc			mCurrAlphaFunc;
450	F32				mCurrAlphaFuncVal;
451
452	LLPointer<LLVertexBuffer>	mBuffer;
453	LLStrider<LLVector3>		mVerticesp;
454	LLStrider<LLVector2>		mTexcoordsp;
455	LLStrider<LLColor4U>		mColorsp;
456	std::vector<LLTexUnit*>		mTexUnits;
457	LLTexUnit*			mDummyTexUnit;
458	std::vector<LLLightState*> mLightState;
459
460	eBlendFactor mCurrBlendColorSFactor;
461	eBlendFactor mCurrBlendColorDFactor;
462	eBlendFactor mCurrBlendAlphaSFactor;
463	eBlendFactor mCurrBlendAlphaDFactor;
464
465	F32				mMaxAnisotropy;
466
467	std::vector<LLVector3> mUIOffset;
468	std::vector<LLVector3> mUIScale;
469
470};
471
472extern F32 gGLModelView[16];
473extern F32 gGLLastModelView[16];
474extern F32 gGLLastProjection[16];
475extern F32 gGLProjection[16];
476extern S32 gGLViewport[4];
477
478extern LLRender gGL;
479
480#endif