llglslshader.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /**
  2. * @file llglslshader.h
  3. * @brief GLSL shader wrappers
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, 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_LLGLSLSHADER_H
  33. #define LL_LLGLSLSHADER_H
  34. #include <vector>
  35. #include "llgl.h"
  36. #include "llrender.h"
  37. #include "llstringtable.h"
  38. class LLRenderTarget;
  39. class LLShaderFeatures
  40. {
  41. public:
  42. LLShaderFeatures();
  43. public:
  44. S32 mIndexedTextureChannels;
  45. bool calculatesLighting;
  46. bool calculatesAtmospherics;
  47. // Implies no transport (it is possible to have neither though):
  48. bool hasLighting;
  49. // Indicates lighting shaders need not be linked in (lighting performed
  50. // directly in alpha shader to match deferred lighting functions):
  51. bool isAlphaLighting;
  52. bool isShiny; // No more used by PBR shaders
  53. bool isFullbright; // Implies no lighting, not used by PBR
  54. bool isSpecular;
  55. bool hasWaterFog; // Implies no gamma, not used by PBR
  56. // Implies no lighting (it is possible to have neither though):
  57. bool hasTransport; // No more used by PBR shaders
  58. bool hasSkinning;
  59. bool hasObjectSkinning;
  60. bool hasAtmospherics;
  61. bool hasGamma;
  62. bool hasSrgb;
  63. bool encodesNormal; // Not used any more by PBR shaders
  64. bool isDeferred;
  65. bool hasShadows;
  66. bool hasAmbientOcclusion;
  67. bool disableTextureIndex;
  68. bool hasAlphaMask;
  69. bool attachNothing;
  70. bool hasScreenSpaceReflections; // For PBR shaders
  71. bool hasReflectionProbes; // For PBR shaders
  72. bool isPBRTerrain; // For PBR shaders
  73. };
  74. class LLGLSLShader
  75. {
  76. protected:
  77. LOG_CLASS(LLGLSLShader);
  78. public:
  79. enum
  80. {
  81. SG_DEFAULT = 0, // Not sky or water specific
  82. SG_SKY,
  83. SG_WATER,
  84. SG_ANY,
  85. SG_COUNT
  86. };
  87. enum EShaderConsts
  88. {
  89. CONST_CLOUD_MOON_DEPTH,
  90. CONST_STAR_DEPTH,
  91. NUM_SHADER_CONSTS
  92. };
  93. LLGLSLShader();
  94. static void initProfile();
  95. static void finishProfile(bool emit_report = true);
  96. LL_INLINE static void startProfile()
  97. {
  98. if (sProfileEnabled && sCurBoundShaderPtr)
  99. {
  100. sCurBoundShaderPtr->placeProfileQuery();
  101. }
  102. }
  103. LL_INLINE static void stopProfile()
  104. {
  105. if (sProfileEnabled && sCurBoundShaderPtr)
  106. {
  107. sCurBoundShaderPtr->readProfileQuery();
  108. }
  109. }
  110. void clearStats();
  111. void dumpStats();
  112. void placeProfileQuery();
  113. void readProfileQuery();
  114. void setup(const char* name, S32 level, const char* vertex_shader,
  115. const char* fragment_shader,
  116. const char* geometry_shader = NULL);
  117. void unload();
  118. typedef std::vector<LLStaticHashedString> hash_vector_t;
  119. bool createShader(hash_vector_t* attributes = NULL,
  120. hash_vector_t* uniforms = NULL,
  121. U32 varying_count = 0, const char** varyings = NULL);
  122. bool attachVertexObject(const char* object);
  123. bool attachFragmentObject(const char* object);
  124. void attachObject(GLuint object);
  125. void attachObjects(GLuint* objects = NULL, S32 count = 0);
  126. bool mapAttributes(const hash_vector_t* attributes);
  127. bool mapUniforms(const hash_vector_t* uniforms);
  128. void mapUniform(S32 index, const hash_vector_t* uniforms);
  129. void uniform1i(U32 index, S32 i);
  130. void uniform1f(U32 index, F32 v);
  131. void uniform2f(U32 index, F32 x, F32 y);
  132. void uniform3f(U32 index, F32 x, F32 y, F32 z);
  133. void uniform4f(U32 index, F32 x, F32 y, F32 z, F32 w);
  134. void uniform1iv(U32 index, U32 count, const S32* i);
  135. void uniform4iv(U32 index, U32 count, const S32* i);
  136. void uniform1fv(U32 index, U32 count, const F32* v);
  137. void uniform2fv(U32 index, U32 count, const F32* v);
  138. void uniform3fv(U32 index, U32 count, const F32* v);
  139. void uniform4fv(U32 index, U32 count, const F32* v);
  140. void uniformMatrix2fv(U32 index, U32 count, GLboolean transpose,
  141. const F32* v);
  142. void uniformMatrix3fv(U32 index, U32 count, GLboolean transpose,
  143. const F32* v);
  144. void uniformMatrix3x4fv(U32 index, U32 count, GLboolean transpose,
  145. const F32* v);
  146. void uniformMatrix4fv(U32 index, U32 count, GLboolean transpose,
  147. const F32* v);
  148. void uniform1i(const LLStaticHashedString& uniform, S32 i);
  149. void uniform1iv(const LLStaticHashedString& uniform, U32 count,
  150. const S32* v);
  151. void uniform4iv(const LLStaticHashedString& uniform, U32 count,
  152. const S32* v);
  153. void uniform2i(const LLStaticHashedString& uniform, S32 i, S32 j);
  154. void uniform1f(const LLStaticHashedString& uniform, F32 v);
  155. void uniform2f(const LLStaticHashedString& uniform, F32 x, F32 y);
  156. void uniform3f(const LLStaticHashedString& uniform, F32 x, F32 y,
  157. F32 z);
  158. void uniform4f(const LLStaticHashedString& uniform, F32 x, F32 y, F32 z,
  159. F32 w);
  160. void uniform1fv(const LLStaticHashedString& uniform, U32 count,
  161. const F32* v);
  162. void uniform2fv(const LLStaticHashedString& uniform, U32 count,
  163. const F32* v);
  164. void uniform3fv(const LLStaticHashedString& uniform, U32 count,
  165. const F32* v);
  166. void uniform4fv(const LLStaticHashedString& uniform, U32 count,
  167. const F32* v);
  168. void uniformMatrix4fv(const LLStaticHashedString& uniform, U32 count,
  169. GLboolean transpose, const F32* v);
  170. void setMinimumAlpha(F32 minimum);
  171. void vertexAttrib4f(U32 index, F32 x, F32 y, F32 z, F32 w);
  172. void vertexAttrib4fv(U32 index, F32* v);
  173. S32 getUniformLocation(const LLStaticHashedString& uniform);
  174. S32 getUniformLocation(U32 index);
  175. S32 getAttribLocation(U32 attrib);
  176. void addConstant(EShaderConsts shader_const);
  177. void addPermutation(const std::string& name, const std::string& value);
  178. typedef fast_hmap<std::string, std::string> defines_map_t;
  179. void addPermutations(const defines_map_t& defines);
  180. // Enable/disable texture channel for specified uniform. If given texture
  181. // uniform is active in the shader, the corresponding channel will be
  182. // active upon return. Returns channel texture is enabled in from [0-MAX).
  183. S32 enableTexture(S32 uniform,
  184. LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE,
  185. LLTexUnit::eTextureColorSpace s = LLTexUnit::TCS_LINEAR);
  186. S32 disableTexture(S32 uniform,
  187. LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE,
  188. LLTexUnit::eTextureColorSpace s = LLTexUnit::TCS_LINEAR);
  189. // Gets the texture channel of the given uniform, or -1 if uniform is not
  190. // used as a texture.
  191. LL_INLINE S32 getTextureChannel(S32 u) const
  192. {
  193. return u >= 0 && u < (S32)mTexture.size() ? mTexture[u] : -1;
  194. }
  195. // bindTexture returns the texture unit we have bound the texture to. You
  196. // can reuse the return value to unbind a texture when required.
  197. S32 bindTexture(S32 uniform, LLGLTexture* texp,
  198. LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE,
  199. LLTexUnit::eTextureColorSpace spc = LLTexUnit::TCS_LINEAR);
  200. LL_INLINE S32 bindTexture(const std::string& uniform, LLGLTexture* texp,
  201. LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE,
  202. LLTexUnit::eTextureColorSpace spc = LLTexUnit::TCS_LINEAR)
  203. {
  204. return bindTexture(getUniformLocation(uniform), texp, mode, spc);
  205. }
  206. // Render targets binding methods, for PBR rendering only.
  207. S32 bindTexture(S32 uniform, LLRenderTarget* targetp, bool depth = false,
  208. LLTexUnit::eTextureFilterOptions mode =
  209. LLTexUnit::TFO_BILINEAR,
  210. U32 index = 0);
  211. LL_INLINE S32 bindTexture(const std::string& uniform,
  212. LLRenderTarget* targetp, bool depth = false,
  213. LLTexUnit::eTextureFilterOptions mode =
  214. LLTexUnit::TFO_BILINEAR, U32 index = 0)
  215. {
  216. return bindTexture(getUniformLocation(uniform), targetp, depth, mode,
  217. index);
  218. }
  219. void bind();
  220. // Helper to conditionally bind mRiggedVariant instead of this
  221. void bind(bool rigged);
  222. // Unbinds any previously bound shader by explicitly binding no shader.
  223. static void unbind();
  224. private:
  225. void unloadInternal();
  226. S32 mapUniformTextureChannel(S32 location, U32 type, S32 size);
  227. // Methods to get locations corresponding to texture and uniform indexes.
  228. // They check for index validity and warn in case of error (also printing
  229. // the line number of the faulty call when gDebugGL is true, and crashing
  230. // voluntarily with an assert for debug builds). HB
  231. S32 getTexture(S32 line, S32 index);
  232. S32 getUniform(S32 line, U32 index);
  233. public:
  234. U32 mMatHash[LLRender::NUM_MATRIX_MODES];
  235. U32 mLightHash;
  236. GLuint mProgramObject;
  237. // Lookup table of attribute enum to attribute channel
  238. std::vector<S32> mAttribute;
  239. // Mask of which reserved attributes are set (lines up with
  240. // LLVertexBuffer::getTypeMask())
  241. U32 mAttributeMask;
  242. S32 mTotalUniformSize;
  243. S32 mActiveTextureChannels;
  244. S32 mShaderLevel;
  245. S32 mShaderGroup;
  246. LLShaderFeatures mFeatures;
  247. std::string mName;
  248. // Lookup map of uniform name to uniform location
  249. LLStaticStringTable<S32> mUniformMap;
  250. // Lookup map of uniform location to uniform name
  251. typedef fast_hmap<S32, std::string> uniforms_map_t;
  252. uniforms_map_t mUniformNameMap;
  253. // Lookup table of uniform enum to uniform location
  254. std::vector<S32> mUniform;
  255. // Lookup table of uniform enum to texture channels
  256. std::vector<S32> mTexture;
  257. // Lookup map of uniform location to last known value
  258. typedef fast_hmap<S32, LLVector4> uniform_value_map_t;
  259. uniform_value_map_t mValue;
  260. typedef std::vector<std::pair<std::string, U32> > files_map_t;
  261. files_map_t mShaderFiles;
  262. defines_map_t mDefines;
  263. // This pointer should be set to whichever shader represents this shader's
  264. // rigged variant
  265. LLGLSLShader* mRiggedVariant;
  266. // Statistics for profiling shader performance
  267. U32 mTimerQuery;
  268. U32 mSamplesQuery;
  269. U32 mPrimitivesQuery;
  270. U64 mTimeElapsed;
  271. U32 mTrianglesDrawn;
  272. U64 mSamplesDrawn;
  273. U32 mDrawCalls;
  274. bool mUniformsDirty;
  275. // *HACK: flag used for optimization in LLDrawPoolAlpha and LLPipeline
  276. bool mCanBindFast;
  277. static std::set<LLGLSLShader*> sInstances;
  278. static LLGLSLShader* sCurBoundShaderPtr;
  279. static GLuint sCurBoundShader;
  280. static bool sProfileEnabled;
  281. // Statistics for profiling shader performance
  282. static U64 sTotalTimeElapsed;
  283. static U32 sTotalTrianglesDrawn;
  284. static U64 sTotalSamplesDrawn;
  285. static U32 sTotalDrawCalls;
  286. };
  287. class LLShaderUniforms
  288. {
  289. public:
  290. LL_INLINE LLShaderUniforms()
  291. : mActive(false)
  292. {
  293. }
  294. LL_INLINE void clear()
  295. {
  296. mIntegers.resize(0);
  297. mFloats.resize(0);
  298. mVectors.resize(0);
  299. mVector3s.resize(0);
  300. mActive = false;
  301. }
  302. LL_INLINE void uniform1i(S32 index, S32 value)
  303. {
  304. mIntegers.push_back({ index, value });
  305. mActive = true;
  306. }
  307. LL_INLINE void uniform1f(S32 index, F32 value)
  308. {
  309. mFloats.push_back({ index, value });
  310. mActive = true;
  311. }
  312. LL_INLINE void uniform4fv(S32 index, const LLVector4& value)
  313. {
  314. mVectors.push_back({ index, value });
  315. mActive = true;
  316. }
  317. LL_INLINE void uniform4fv(S32 index, const F32* value)
  318. {
  319. mVectors.push_back({ index, LLVector4(value) });
  320. mActive = true;
  321. }
  322. LL_INLINE void uniform3fv(S32 index, const LLVector3& value)
  323. {
  324. mVector3s.push_back({ index, value });
  325. mActive = true;
  326. }
  327. LL_INLINE void uniform3fv(S32 index, const F32* value)
  328. {
  329. mVector3s.push_back({ index, LLVector3(value) });
  330. mActive = true;
  331. }
  332. void apply(LLGLSLShader* shader);
  333. private:
  334. template<typename T>
  335. struct UniformSetting
  336. {
  337. S32 mUniform;
  338. T mValue;
  339. };
  340. typedef UniformSetting<S32> IntSetting;
  341. typedef UniformSetting<F32> FloatSetting;
  342. typedef UniformSetting<LLVector4> VectorSetting;
  343. typedef UniformSetting<LLVector3> Vector3Setting;
  344. std::vector<IntSetting> mIntegers;
  345. std::vector<FloatSetting> mFloats;
  346. std::vector<VectorSetting> mVectors;
  347. std::vector<Vector3Setting> mVector3s;
  348. bool mActive;
  349. };
  350. // UI shader
  351. extern LLGLSLShader gUIProgram;
  352. // Output vec4(color.rgb, color.a * tex0[tc0].a)
  353. extern LLGLSLShader gSolidColorProgram;
  354. // Alpha mask shader (declared here so llappearance can access properly)
  355. extern LLGLSLShader gAlphaMaskProgram;
  356. #endif