PageRenderTime 35ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #ifndef LL_LLGLRENDER_H
  32. #define LL_LLGLRENDER_H
  33. //#include "linden_common.h"
  34. #include "v2math.h"
  35. #include "v3math.h"
  36. #include "v4coloru.h"
  37. #include "v4math.h"
  38. #include "llstrider.h"
  39. #include "llpointer.h"
  40. #include "llglheaders.h"
  41. #include "llmatrix4a.h"
  42. #include "glh/glh_linear.h"
  43. class LLVertexBuffer;
  44. class LLCubeMap;
  45. class LLImageGL;
  46. class LLRenderTarget;
  47. class LLTexture ;
  48. #define LL_MATRIX_STACK_DEPTH 32
  49. class LLTexUnit
  50. {
  51. friend class LLRender;
  52. public:
  53. static U32 sWhiteTexture;
  54. typedef enum
  55. {
  56. TT_TEXTURE = 0, // Standard 2D Texture
  57. TT_RECT_TEXTURE, // Non power of 2 texture
  58. TT_CUBE_MAP, // 6-sided cube map texture
  59. TT_MULTISAMPLE_TEXTURE, // see GL_ARB_texture_multisample
  60. TT_NONE // No texture type is currently enabled
  61. } eTextureType;
  62. typedef enum
  63. {
  64. TAM_WRAP = 0, // Standard 2D Texture
  65. TAM_MIRROR, // Non power of 2 texture
  66. TAM_CLAMP // No texture type is currently enabled
  67. } eTextureAddressMode;
  68. typedef enum
  69. { // Note: If mipmapping or anisotropic are not enabled or supported it should fall back gracefully
  70. TFO_POINT = 0, // Equal to: min=point, mag=point, mip=none.
  71. TFO_BILINEAR, // Equal to: min=linear, mag=linear, mip=point.
  72. TFO_TRILINEAR, // Equal to: min=linear, mag=linear, mip=linear.
  73. TFO_ANISOTROPIC // Equal to: min=anisotropic, max=anisotropic, mip=linear.
  74. } eTextureFilterOptions;
  75. typedef enum
  76. {
  77. TB_REPLACE = 0,
  78. TB_ADD,
  79. TB_MULT,
  80. TB_MULT_X2,
  81. TB_ALPHA_BLEND,
  82. TB_COMBINE // Doesn't need to be set directly, setTexture___Blend() set TB_COMBINE automatically
  83. } eTextureBlendType;
  84. typedef enum
  85. {
  86. TBO_REPLACE = 0, // Use Source 1
  87. TBO_MULT, // Multiply: ( Source1 * Source2 )
  88. TBO_MULT_X2, // Multiply then scale by 2: ( 2.0 * ( Source1 * Source2 ) )
  89. TBO_MULT_X4, // Multiply then scale by 4: ( 4.0 * ( Source1 * Source2 ) )
  90. TBO_ADD, // Add: ( Source1 + Source2 )
  91. TBO_ADD_SIGNED, // Add then subtract 0.5: ( ( Source1 + Source2 ) - 0.5 )
  92. TBO_SUBTRACT, // Subtract Source2 from Source1: ( Source1 - Source2 )
  93. TBO_LERP_VERT_ALPHA, // Interpolate based on Vertex Alpha (VA): ( Source1 * VA + Source2 * (1-VA) )
  94. TBO_LERP_TEX_ALPHA, // Interpolate based on Texture Alpha (TA): ( Source1 * TA + Source2 * (1-TA) )
  95. TBO_LERP_PREV_ALPHA, // Interpolate based on Previous Alpha (PA): ( Source1 * PA + Source2 * (1-PA) )
  96. TBO_LERP_CONST_ALPHA, // Interpolate based on Const Alpha (CA): ( Source1 * CA + Source2 * (1-CA) )
  97. TBO_LERP_VERT_COLOR // Interpolate based on Vertex Col (VC): ( Source1 * VC + Source2 * (1-VC) )
  98. // *Note* TBO_LERP_VERTEX_COLOR only works with setTextureColorBlend(),
  99. // and falls back to TBO_LERP_VERTEX_ALPHA for setTextureAlphaBlend().
  100. } eTextureBlendOp;
  101. typedef enum
  102. {
  103. TBS_PREV_COLOR = 0, // Color from the previous texture stage
  104. TBS_PREV_ALPHA,
  105. TBS_ONE_MINUS_PREV_COLOR,
  106. TBS_ONE_MINUS_PREV_ALPHA,
  107. TBS_TEX_COLOR, // Color from the texture bound to this stage
  108. TBS_TEX_ALPHA,
  109. TBS_ONE_MINUS_TEX_COLOR,
  110. TBS_ONE_MINUS_TEX_ALPHA,
  111. TBS_VERT_COLOR, // The vertex color currently set
  112. TBS_VERT_ALPHA,
  113. TBS_ONE_MINUS_VERT_COLOR,
  114. TBS_ONE_MINUS_VERT_ALPHA,
  115. TBS_CONST_COLOR, // The constant color value currently set
  116. TBS_CONST_ALPHA,
  117. TBS_ONE_MINUS_CONST_COLOR,
  118. TBS_ONE_MINUS_CONST_ALPHA
  119. } eTextureBlendSrc;
  120. LLTexUnit(S32 index);
  121. // Refreshes renderer state of the texture unit to the cached values
  122. // Needed when the render context has changed and invalidated the current state
  123. void refreshState(void);
  124. // returns the index of this texture unit
  125. S32 getIndex(void) const { return mIndex; }
  126. // Sets this tex unit to be the currently active one
  127. void activate(void);
  128. // Enables this texture unit for the given texture type
  129. // (automatically disables any previously enabled texture type)
  130. void enable(eTextureType type);
  131. // Disables the current texture unit
  132. void disable(void);
  133. // Binds the LLImageGL to this texture unit
  134. // (automatically enables the unit for the LLImageGL's texture type)
  135. bool bind(LLImageGL* texture, bool for_rendering = false, bool forceBind = false);
  136. bool bind(LLTexture* texture, bool for_rendering = false, bool forceBind = false);
  137. // Binds a cubemap to this texture unit
  138. // (automatically enables the texture unit for cubemaps)
  139. bool bind(LLCubeMap* cubeMap);
  140. // Binds a render target to this texture unit
  141. // (automatically enables the texture unit for the RT's texture type)
  142. bool bind(LLRenderTarget * renderTarget, bool bindDepth = false);
  143. // Manually binds a texture to the texture unit
  144. // (automatically enables the tex unit for the given texture type)
  145. bool bindManual(eTextureType type, U32 texture, bool hasMips = false);
  146. // Unbinds the currently bound texture of the given type
  147. // (only if there's a texture of the given type currently bound)
  148. void unbind(eTextureType type);
  149. // Sets the addressing mode used to sample the texture
  150. // Warning: this stays set for the bound texture forever,
  151. // make sure you want to permanently change the address mode for the bound texture.
  152. void setTextureAddressMode(eTextureAddressMode mode);
  153. // Sets the filtering options used to sample the texture
  154. // Warning: this stays set for the bound texture forever,
  155. // make sure you want to permanently change the filtering for the bound texture.
  156. void setTextureFilteringOption(LLTexUnit::eTextureFilterOptions option);
  157. void setTextureBlendType(eTextureBlendType type);
  158. inline void setTextureColorBlend(eTextureBlendOp op, eTextureBlendSrc src1, eTextureBlendSrc src2 = TBS_PREV_COLOR)
  159. { setTextureCombiner(op, src1, src2, false); }
  160. // NOTE: If *_COLOR enums are passed to src1 or src2, the corresponding *_ALPHA enum will be used instead.
  161. inline void setTextureAlphaBlend(eTextureBlendOp op, eTextureBlendSrc src1, eTextureBlendSrc src2 = TBS_PREV_ALPHA)
  162. { setTextureCombiner(op, src1, src2, true); }
  163. static U32 getInternalType(eTextureType type);
  164. U32 getCurrTexture(void) { return mCurrTexture; }
  165. eTextureType getCurrType(void) { return mCurrTexType; }
  166. void setHasMipMaps(bool hasMips) { mHasMipMaps = hasMips; }
  167. protected:
  168. S32 mIndex;
  169. U32 mCurrTexture;
  170. eTextureType mCurrTexType;
  171. eTextureBlendType mCurrBlendType;
  172. eTextureBlendOp mCurrColorOp;
  173. eTextureBlendSrc mCurrColorSrc1;
  174. eTextureBlendSrc mCurrColorSrc2;
  175. eTextureBlendOp mCurrAlphaOp;
  176. eTextureBlendSrc mCurrAlphaSrc1;
  177. eTextureBlendSrc mCurrAlphaSrc2;
  178. S32 mCurrColorScale;
  179. S32 mCurrAlphaScale;
  180. bool mHasMipMaps;
  181. void debugTextureUnit(void);
  182. void setColorScale(S32 scale);
  183. void setAlphaScale(S32 scale);
  184. GLint getTextureSource(eTextureBlendSrc src);
  185. GLint getTextureSourceType(eTextureBlendSrc src, bool isAlpha = false);
  186. void setTextureCombiner(eTextureBlendOp op, eTextureBlendSrc src1, eTextureBlendSrc src2, bool isAlpha = false);
  187. };
  188. class LLLightState
  189. {
  190. public:
  191. LLLightState(S32 index);
  192. void enable();
  193. void disable();
  194. void setDiffuse(const LLColor4& diffuse);
  195. void setAmbient(const LLColor4& ambient);
  196. void setSpecular(const LLColor4& specular);
  197. void setPosition(const LLVector4& position);
  198. void setConstantAttenuation(const F32& atten);
  199. void setLinearAttenuation(const F32& atten);
  200. void setQuadraticAttenuation(const F32& atten);
  201. void setSpotExponent(const F32& exponent);
  202. void setSpotCutoff(const F32& cutoff);
  203. void setSpotDirection(const LLVector3& direction);
  204. protected:
  205. friend class LLRender;
  206. S32 mIndex;
  207. bool mEnabled;
  208. LLColor4 mDiffuse;
  209. LLColor4 mAmbient;
  210. LLColor4 mSpecular;
  211. LLVector4 mPosition;
  212. LLVector3 mSpotDirection;
  213. F32 mConstantAtten;
  214. F32 mLinearAtten;
  215. F32 mQuadraticAtten;
  216. F32 mSpotExponent;
  217. F32 mSpotCutoff;
  218. };
  219. class LLRender
  220. {
  221. friend class LLTexUnit;
  222. public:
  223. typedef enum {
  224. TRIANGLES = 0,
  225. TRIANGLE_STRIP,
  226. TRIANGLE_FAN,
  227. POINTS,
  228. LINES,
  229. LINE_STRIP,
  230. QUADS,
  231. LINE_LOOP,
  232. NUM_MODES
  233. } eGeomModes;
  234. typedef enum
  235. {
  236. CF_NEVER = 0,
  237. CF_ALWAYS,
  238. CF_LESS,
  239. CF_LESS_EQUAL,
  240. CF_EQUAL,
  241. CF_NOT_EQUAL,
  242. CF_GREATER_EQUAL,
  243. CF_GREATER,
  244. CF_DEFAULT
  245. } eCompareFunc;
  246. typedef enum
  247. {
  248. BT_ALPHA = 0,
  249. BT_ADD,
  250. BT_ADD_WITH_ALPHA, // Additive blend modulated by the fragment's alpha.
  251. BT_MULT,
  252. BT_MULT_ALPHA,
  253. BT_MULT_X2,
  254. BT_REPLACE
  255. } eBlendType;
  256. typedef enum
  257. {
  258. BF_ONE = 0,
  259. BF_ZERO,
  260. BF_DEST_COLOR,
  261. BF_SOURCE_COLOR,
  262. BF_ONE_MINUS_DEST_COLOR,
  263. BF_ONE_MINUS_SOURCE_COLOR,
  264. BF_DEST_ALPHA,
  265. BF_SOURCE_ALPHA,
  266. BF_ONE_MINUS_DEST_ALPHA,
  267. BF_ONE_MINUS_SOURCE_ALPHA,
  268. BF_UNDEF
  269. } eBlendFactor;
  270. typedef enum
  271. {
  272. MM_MODELVIEW = 0,
  273. MM_PROJECTION,
  274. MM_TEXTURE0,
  275. MM_TEXTURE1,
  276. MM_TEXTURE2,
  277. MM_TEXTURE3,
  278. NUM_MATRIX_MODES,
  279. MM_TEXTURE
  280. } eMatrixMode;
  281. LLRender();
  282. ~LLRender();
  283. void init() ;
  284. void shutdown();
  285. // Refreshes renderer state to the cached values
  286. // Needed when the render context has changed and invalidated the current state
  287. void refreshState(void);
  288. void translatef(const GLfloat& x, const GLfloat& y, const GLfloat& z);
  289. void scalef(const GLfloat& x, const GLfloat& y, const GLfloat& z);
  290. void rotatef(const GLfloat& a, const GLfloat& x, const GLfloat& y, const GLfloat& z);
  291. void ortho(F32 left, F32 right, F32 bottom, F32 top, F32 zNear, F32 zFar);
  292. void pushMatrix();
  293. void popMatrix();
  294. void loadMatrix(const GLfloat* m);
  295. void loadIdentity();
  296. void multMatrix(const GLfloat* m);
  297. void matrixMode(U32 mode);
  298. const glh::matrix4f& getModelviewMatrix();
  299. const glh::matrix4f& getProjectionMatrix();
  300. void syncMatrices();
  301. void syncLightState();
  302. void translateUI(F32 x, F32 y, F32 z);
  303. void scaleUI(F32 x, F32 y, F32 z);
  304. void pushUIMatrix();
  305. void popUIMatrix();
  306. void loadUIIdentity();
  307. LLVector3 getUITranslation();
  308. LLVector3 getUIScale();
  309. void flush();
  310. void begin(const GLuint& mode);
  311. void end();
  312. void vertex2i(const GLint& x, const GLint& y);
  313. void vertex2f(const GLfloat& x, const GLfloat& y);
  314. void vertex3f(const GLfloat& x, const GLfloat& y, const GLfloat& z);
  315. void vertex2fv(const GLfloat* v);
  316. void vertex3fv(const GLfloat* v);
  317. void texCoord2i(const GLint& x, const GLint& y);
  318. void texCoord2f(const GLfloat& x, const GLfloat& y);
  319. void texCoord2fv(const GLfloat* tc);
  320. void color4ub(const GLubyte& r, const GLubyte& g, const GLubyte& b, const GLubyte& a);
  321. void color4f(const GLfloat& r, const GLfloat& g, const GLfloat& b, const GLfloat& a);
  322. void color4fv(const GLfloat* c);
  323. void color3f(const GLfloat& r, const GLfloat& g, const GLfloat& b);
  324. void color3fv(const GLfloat* c);
  325. void color4ubv(const GLubyte* c);
  326. void diffuseColor3f(F32 r, F32 g, F32 b);
  327. void diffuseColor3fv(const F32* c);
  328. void diffuseColor4f(F32 r, F32 g, F32 b, F32 a);
  329. void diffuseColor4fv(const F32* c);
  330. void diffuseColor4ubv(const U8* c);
  331. void vertexBatchPreTransformed(LLVector3* verts, S32 vert_count);
  332. void vertexBatchPreTransformed(LLVector3* verts, LLVector2* uvs, S32 vert_count);
  333. void vertexBatchPreTransformed(LLVector3* verts, LLVector2* uvs, LLColor4U*, S32 vert_count);
  334. void setColorMask(bool writeColor, bool writeAlpha);
  335. void setColorMask(bool writeColorR, bool writeColorG, bool writeColorB, bool writeAlpha);
  336. void setSceneBlendType(eBlendType type);
  337. void setAlphaRejectSettings(eCompareFunc func, F32 value = 0.01f);
  338. // applies blend func to both color and alpha
  339. void blendFunc(eBlendFactor sfactor, eBlendFactor dfactor);
  340. // applies separate blend functions to color and alpha
  341. void blendFunc(eBlendFactor color_sfactor, eBlendFactor color_dfactor,
  342. eBlendFactor alpha_sfactor, eBlendFactor alpha_dfactor);
  343. LLLightState* getLight(U32 index);
  344. void setAmbientLightColor(const LLColor4& color);
  345. LLTexUnit* getTexUnit(U32 index);
  346. U32 getCurrentTexUnitIndex(void) const { return mCurrTextureUnitIndex; }
  347. bool verifyTexUnitActive(U32 unitToVerify);
  348. void debugTexUnits(void);
  349. void clearErrors();
  350. struct Vertex
  351. {
  352. GLfloat v[3];
  353. GLubyte c[4];
  354. GLfloat uv[2];
  355. };
  356. public:
  357. static U32 sUICalls;
  358. static U32 sUIVerts;
  359. static bool sGLCoreProfile;
  360. private:
  361. friend class LLLightState;
  362. U32 mMatrixMode;
  363. U32 mMatIdx[NUM_MATRIX_MODES];
  364. U32 mMatHash[NUM_MATRIX_MODES];
  365. glh::matrix4f mMatrix[NUM_MATRIX_MODES][LL_MATRIX_STACK_DEPTH];
  366. U32 mCurMatHash[NUM_MATRIX_MODES];
  367. U32 mLightHash;
  368. LLColor4 mAmbientLightColor;
  369. bool mDirty;
  370. U32 mQuadCycle;
  371. U32 mCount;
  372. U32 mMode;
  373. U32 mCurrTextureUnitIndex;
  374. bool mCurrColorMask[4];
  375. eCompareFunc mCurrAlphaFunc;
  376. F32 mCurrAlphaFuncVal;
  377. LLPointer<LLVertexBuffer> mBuffer;
  378. LLStrider<LLVector3> mVerticesp;
  379. LLStrider<LLVector2> mTexcoordsp;
  380. LLStrider<LLColor4U> mColorsp;
  381. std::vector<LLTexUnit*> mTexUnits;
  382. LLTexUnit* mDummyTexUnit;
  383. std::vector<LLLightState*> mLightState;
  384. eBlendFactor mCurrBlendColorSFactor;
  385. eBlendFactor mCurrBlendColorDFactor;
  386. eBlendFactor mCurrBlendAlphaSFactor;
  387. eBlendFactor mCurrBlendAlphaDFactor;
  388. F32 mMaxAnisotropy;
  389. std::vector<LLVector3> mUIOffset;
  390. std::vector<LLVector3> mUIScale;
  391. };
  392. extern F32 gGLModelView[16];
  393. extern F32 gGLLastModelView[16];
  394. extern F32 gGLLastProjection[16];
  395. extern F32 gGLProjection[16];
  396. extern S32 gGLViewport[4];
  397. extern LLRender gGL;
  398. #endif