llglslshader.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. /**
  2. * @file llglslshader.cpp
  3. * @brief GLSL helper functions and state.
  4. *
  5. * $LicenseInfo:firstyear=2005&license=viewergpl$
  6. *
  7. * Copyright (c) 2005-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. #include "linden_common.h"
  33. #include <utility>
  34. #include "llglslshader.h"
  35. #include "llshadermgr.h"
  36. #include "llrendertarget.h"
  37. #include "llvertexbuffer.h"
  38. #if LL_DARWIN
  39. # include "OpenGL/OpenGL.h"
  40. #endif
  41. GLuint LLGLSLShader::sCurBoundShader = 0;
  42. LLGLSLShader* LLGLSLShader::sCurBoundShaderPtr = NULL;
  43. bool LLGLSLShader::sProfileEnabled = false;
  44. std::set<LLGLSLShader*> LLGLSLShader::sInstances;
  45. U64 LLGLSLShader::sTotalTimeElapsed = 0;
  46. U32 LLGLSLShader::sTotalTrianglesDrawn = 0;
  47. U64 LLGLSLShader::sTotalSamplesDrawn = 0;
  48. U32 LLGLSLShader::sTotalDrawCalls = 0;
  49. // UI shaders
  50. LLGLSLShader gUIProgram;
  51. LLGLSLShader gSolidColorProgram;
  52. // Shader constants: keep in sync with LLGLSLShader::EShaderConsts !
  53. static const std::string sShaderConstsKey[LLGLSLShader::NUM_SHADER_CONSTS] =
  54. {
  55. "LL_SHADER_CONST_CLOUD_MOON_DEPTH",
  56. "LL_SHADER_CONST_STAR_DEPTH"
  57. };
  58. static const std::string sShaderConstsVal[LLGLSLShader::NUM_SHADER_CONSTS] =
  59. {
  60. "0.99998", // SHADER_CONST_CLOUD_MOON_DEPTH
  61. "0.99999" // SHADER_CONST_STAR_DEPTH
  62. };
  63. //-----------------------------------------------------------------------------
  64. // LLShaderFeatures class
  65. //-----------------------------------------------------------------------------
  66. LLShaderFeatures::LLShaderFeatures()
  67. : mIndexedTextureChannels(0),
  68. calculatesLighting(false),
  69. calculatesAtmospherics(false),
  70. hasLighting(false),
  71. isAlphaLighting(false),
  72. isShiny(false), // No more used by PBR shaders
  73. isFullbright(false), // No more used by PBR shaders
  74. isSpecular(false),
  75. hasWaterFog(false),
  76. hasTransport(false),
  77. hasSkinning(false),
  78. hasObjectSkinning(false),
  79. hasAtmospherics(false),
  80. hasGamma(false),
  81. hasSrgb(false),
  82. encodesNormal(false),
  83. isDeferred(false),
  84. hasShadows(false),
  85. hasAmbientOcclusion(false),
  86. disableTextureIndex(false),
  87. hasAlphaMask(false),
  88. attachNothing(false),
  89. hasScreenSpaceReflections(false), // For PBR shaders
  90. hasReflectionProbes(false), // For PBR shaders
  91. isPBRTerrain(false) // For PBR shaders
  92. {
  93. }
  94. //-----------------------------------------------------------------------------
  95. // LLGLSLShader class
  96. //-----------------------------------------------------------------------------
  97. //static
  98. void LLGLSLShader::initProfile()
  99. {
  100. sProfileEnabled = true;
  101. sTotalTimeElapsed = 0;
  102. sTotalTrianglesDrawn = 0;
  103. sTotalSamplesDrawn = 0;
  104. sTotalDrawCalls = 0;
  105. for (std::set<LLGLSLShader*>::iterator iter = sInstances.begin(),
  106. end = sInstances.end();
  107. iter != end; ++iter)
  108. {
  109. (*iter)->clearStats();
  110. }
  111. }
  112. struct LLGLSLShaderCompareTimeElapsed
  113. {
  114. LL_INLINE bool operator()(const LLGLSLShader* const& lhs,
  115. const LLGLSLShader* const& rhs)
  116. {
  117. return lhs->mTimeElapsed < rhs->mTimeElapsed;
  118. }
  119. };
  120. //static
  121. void LLGLSLShader::finishProfile(bool emit_report)
  122. {
  123. sProfileEnabled = false;
  124. if (!emit_report)
  125. {
  126. return;
  127. }
  128. std::vector<LLGLSLShader*> sorted;
  129. for (std::set<LLGLSLShader*>::iterator iter = sInstances.begin(),
  130. end = sInstances.end();
  131. iter != end; ++iter)
  132. {
  133. sorted.push_back(*iter);
  134. }
  135. std::sort(sorted.begin(), sorted.end(), LLGLSLShaderCompareTimeElapsed());
  136. for (std::vector<LLGLSLShader*>::iterator iter = sorted.begin(),
  137. end = sorted.end();
  138. iter != end; ++iter)
  139. {
  140. (*iter)->dumpStats();
  141. }
  142. llinfos << "\nTotal rendering time: "
  143. << llformat("%.4f ms", sTotalTimeElapsed / 1000000.f)
  144. << "\nTotal samples drawn: "
  145. << llformat("%.4f million", sTotalSamplesDrawn / 1000000.f)
  146. << "\nTotal triangles drawn: "
  147. << llformat("%.3f million", sTotalTrianglesDrawn / 1000000.f)
  148. << llendl;
  149. }
  150. void LLGLSLShader::clearStats()
  151. {
  152. mTrianglesDrawn = 0;
  153. mTimeElapsed = 0;
  154. mSamplesDrawn = 0;
  155. mDrawCalls = 0;
  156. }
  157. void LLGLSLShader::dumpStats()
  158. {
  159. if (mDrawCalls > 0)
  160. {
  161. llinfos << "\n=============================================\n"
  162. << mName;
  163. for (U32 i = 0, count = mShaderFiles.size(); i < count; ++i)
  164. {
  165. llcont << "\n" << mShaderFiles[i].first;
  166. }
  167. llcont << "\n=============================================";
  168. F32 ms = mTimeElapsed / 1000000.f;
  169. F32 seconds = ms / 1000.f;
  170. F32 pct_tris = (F32)mTrianglesDrawn / (F32)sTotalTrianglesDrawn * 100.f;
  171. F32 tris_sec = (F32)(mTrianglesDrawn / 1000000.0);
  172. tris_sec /= seconds;
  173. F32 pct_samples = (F32)((F64)mSamplesDrawn / (F64)sTotalSamplesDrawn) * 100.f;
  174. F32 samples_sec = (F32)mSamplesDrawn / 1000000000.0;
  175. samples_sec /= seconds;
  176. F32 pct_calls = (F32) mDrawCalls / (F32)sTotalDrawCalls * 100.f;
  177. U32 avg_batch = mTrianglesDrawn / mDrawCalls;
  178. llcont << "\nTriangles Drawn: " << mTrianglesDrawn << " "
  179. << llformat("(%.2f pct of total, %.3f million/sec)", pct_tris,
  180. tris_sec )
  181. << "\nDraw Calls: " << mDrawCalls << " "
  182. << llformat("(%.2f pct of total, avg %d tris/call)", pct_calls,
  183. avg_batch)
  184. << "\nSamplesDrawn: " << mSamplesDrawn << " "
  185. << llformat("(%.2f pct of total, %.3f billion/sec)",
  186. pct_samples, samples_sec)
  187. << "\nTime Elapsed: " << mTimeElapsed << " "
  188. << llformat("(%.2f pct of total, %.5f ms)\n",
  189. (F32)((F64)mTimeElapsed/(F64)sTotalTimeElapsed) * 100.f,
  190. ms)
  191. << llendl;
  192. }
  193. }
  194. void LLGLSLShader::placeProfileQuery()
  195. {
  196. if (sProfileEnabled)
  197. {
  198. if (mTimerQuery == 0)
  199. {
  200. glGenQueries(1, &mSamplesQuery);
  201. glGenQueries(1, &mTimerQuery);
  202. glGenQueries(1, &mPrimitivesQuery);
  203. }
  204. glBeginQuery(GL_SAMPLES_PASSED, mSamplesQuery);
  205. glBeginQuery(GL_TIME_ELAPSED, mTimerQuery);
  206. glBeginQuery(GL_PRIMITIVES_GENERATED, mPrimitivesQuery);
  207. }
  208. }
  209. void LLGLSLShader::readProfileQuery()
  210. {
  211. if (!sProfileEnabled)
  212. {
  213. return;
  214. }
  215. glEndQuery(GL_TIME_ELAPSED);
  216. glEndQuery(GL_SAMPLES_PASSED);
  217. glEndQuery(GL_PRIMITIVES_GENERATED);
  218. GLuint64 time_elapsed = 0;
  219. glGetQueryObjectui64v(mTimerQuery, GL_QUERY_RESULT, &time_elapsed);
  220. GLuint64 samples_passed = 0;
  221. glGetQueryObjectui64v(mSamplesQuery, GL_QUERY_RESULT, &samples_passed);
  222. stop_glerror();
  223. GLuint64 primitives = 0;
  224. glGetQueryObjectui64v(mPrimitivesQuery, GL_QUERY_RESULT, &primitives);
  225. sTotalTimeElapsed += time_elapsed;
  226. mTimeElapsed += time_elapsed;
  227. sTotalSamplesDrawn += samples_passed;
  228. mSamplesDrawn += samples_passed;
  229. U32 tri_count = (U32)primitives / 3;
  230. mTrianglesDrawn += tri_count;
  231. sTotalTrianglesDrawn += tri_count;
  232. ++sTotalDrawCalls;
  233. ++mDrawCalls;
  234. }
  235. LLGLSLShader::LLGLSLShader()
  236. : mProgramObject(0),
  237. mAttributeMask(0),
  238. mTotalUniformSize(0),
  239. mActiveTextureChannels(0),
  240. mShaderLevel(0),
  241. mShaderGroup(SG_DEFAULT),
  242. mUniformsDirty(true),
  243. mCanBindFast(false),
  244. mTimerQuery(0),
  245. mSamplesQuery(0),
  246. mPrimitivesQuery(0),
  247. mRiggedVariant(NULL)
  248. {
  249. }
  250. void LLGLSLShader::setup(const char* name, S32 level,
  251. const char* vertex_shader,
  252. const char* fragment_shader,
  253. const char* geometry_shader)
  254. {
  255. // NOTE: sadly, vertex shader names do not all end with "V.glsl", and
  256. // fragment shader names do not all end with "F.glsl", so only check for
  257. // contradictory naming...
  258. if (strstr(vertex_shader, "F.glsl"))
  259. {
  260. llerrs << "Passing a fragment shader name for the vertex shader: "
  261. << vertex_shader << llendl;
  262. }
  263. if (strstr(fragment_shader, "V.glsl"))
  264. {
  265. llerrs << "Passing a vertex shader name for the fragment shader: "
  266. << fragment_shader << llendl;
  267. }
  268. mName = name;
  269. mShaderLevel = level;
  270. mShaderFiles.clear();
  271. mShaderFiles.emplace_back(vertex_shader, GL_VERTEX_SHADER);
  272. if (geometry_shader)
  273. {
  274. mShaderFiles.emplace_back(geometry_shader, GL_GEOMETRY_SHADER);
  275. }
  276. mShaderFiles.emplace_back(fragment_shader, GL_FRAGMENT_SHADER);
  277. // Reset everything else to the default values
  278. mDefines.clear();
  279. mUniformsDirty = true;
  280. clearStats();
  281. mShaderGroup = SG_DEFAULT;
  282. mActiveTextureChannels = 0;
  283. mTimerQuery = 0;
  284. mSamplesQuery = 0;
  285. mAttributeMask = 0;
  286. mTotalUniformSize = 0;
  287. mCanBindFast = false;
  288. mFeatures.mIndexedTextureChannels = 0;
  289. mFeatures.calculatesLighting = mFeatures.calculatesAtmospherics =
  290. mFeatures.hasLighting = mFeatures.isAlphaLighting = mFeatures.isShiny =
  291. mFeatures.isFullbright = mFeatures.isSpecular = mFeatures.hasWaterFog =
  292. mFeatures.hasTransport = mFeatures.hasSkinning =
  293. mFeatures.hasObjectSkinning = mFeatures.hasAtmospherics =
  294. mFeatures.hasGamma = mFeatures.hasSrgb = mFeatures.encodesNormal =
  295. mFeatures.isDeferred = mFeatures.hasShadows =
  296. mFeatures.hasAmbientOcclusion = mFeatures.disableTextureIndex =
  297. mFeatures.hasAlphaMask = mFeatures.attachNothing =
  298. mFeatures.hasScreenSpaceReflections = mFeatures.hasReflectionProbes =
  299. mFeatures.isPBRTerrain = false;
  300. }
  301. void LLGLSLShader::unload()
  302. {
  303. mShaderFiles.clear();
  304. mDefines.clear();
  305. unloadInternal();
  306. }
  307. void LLGLSLShader::unloadInternal()
  308. {
  309. sInstances.erase(this);
  310. clear_glerror();
  311. mAttribute.clear();
  312. mTexture.clear();
  313. mUniform.clear();
  314. if (mProgramObject)
  315. {
  316. GLuint obj[1024];
  317. GLsizei count;
  318. glGetAttachedShaders(mProgramObject, sizeof(obj) / sizeof(obj[0]),
  319. &count, obj);
  320. for (GLsizei i = 0; i < count; ++i)
  321. {
  322. glDetachShader(mProgramObject, obj[i]);
  323. }
  324. for (GLsizei i = 0; i < count; ++i)
  325. {
  326. if (glIsShader(obj[i]))
  327. {
  328. glDeleteShader(obj[i]);
  329. }
  330. }
  331. glDeleteProgram(mProgramObject);
  332. mProgramObject = 0;
  333. }
  334. if (mTimerQuery)
  335. {
  336. glDeleteQueries(1, &mTimerQuery);
  337. mTimerQuery = 0;
  338. }
  339. if (mSamplesQuery)
  340. {
  341. glDeleteQueries(1, &mSamplesQuery);
  342. mSamplesQuery = 0;
  343. }
  344. #if LL_DARWIN
  345. // *HACK: to stop Apple complaining
  346. clear_glerror();
  347. #else
  348. stop_glerror();
  349. #endif
  350. }
  351. bool LLGLSLShader::createShader(hash_vector_t* attributes,
  352. hash_vector_t* uniforms, U32 varying_count,
  353. const char** varyings)
  354. {
  355. unloadInternal();
  356. sInstances.insert(this);
  357. // Reloading, reset matrix hash values
  358. for (U32 i = 0; i < LLRender::NUM_MATRIX_MODES; ++i)
  359. {
  360. mMatHash[i] = 0xFFFFFFFF;
  361. }
  362. mLightHash = 0xFFFFFFFF;
  363. llassert_always(!mShaderFiles.empty());
  364. // Create program
  365. mProgramObject = glCreateProgram();
  366. if (mProgramObject == 0)
  367. {
  368. // This should not happen if shader-related extensions, like
  369. // ARB_vertex_shader, exist.
  370. llwarns << "Failed to create handle for shader: " << mName << llendl;
  371. return false;
  372. }
  373. bool success = true;
  374. LLShaderMgr* shadermgr = LLShaderMgr::getInstance();
  375. #if LL_DARWIN
  376. // Work-around missing mix(vec3,vec3,bvec3)
  377. mDefines["OLD_SELECT"] = "1";
  378. #endif
  379. // Compile new source
  380. for (files_map_t::iterator it = mShaderFiles.begin();
  381. it != mShaderFiles.end(); ++it)
  382. {
  383. GLuint shaderhandle =
  384. shadermgr->loadShaderFile(it->first, mShaderLevel, it->second,
  385. &mDefines,
  386. mFeatures.mIndexedTextureChannels);
  387. llinfos << "Creating shader: " << mName << " - Level: " << mShaderLevel
  388. << " - File: " << it->first << LL_ENDL;
  389. if (shaderhandle)
  390. {
  391. attachObject(shaderhandle);
  392. }
  393. else
  394. {
  395. success = false;
  396. }
  397. }
  398. // Attach existing objects
  399. if (!shadermgr->attachShaderFeatures(this))
  400. {
  401. unloadInternal();
  402. return false;
  403. }
  404. if (gGLManager.mGLSLVersionMajor < 2 && gGLManager.mGLSLVersionMinor < 3)
  405. {
  406. // Indexed texture rendering requires GLSL 1.3 or later
  407. // attachShaderFeatures may have set the number of indexed texture
  408. // channels, so set to 1 again
  409. mFeatures.mIndexedTextureChannels =
  410. llmin(mFeatures.mIndexedTextureChannels, 1);
  411. }
  412. // Map attributes and uniforms
  413. if (success)
  414. {
  415. success = mapAttributes(attributes);
  416. }
  417. else
  418. {
  419. llwarns << "Failed to map attributes for: " << mName << llendl;
  420. }
  421. if (success)
  422. {
  423. success = mapUniforms(uniforms);
  424. }
  425. else
  426. {
  427. llwarns << "Failed to map uniforms for: " << mName << llendl;
  428. }
  429. if (!success)
  430. {
  431. // Try again using a lower shader level;
  432. if (mShaderLevel > 0)
  433. {
  434. llwarns << "Failed to link using shader level "
  435. << mShaderLevel << " trying again using shader level "
  436. << mShaderLevel - 1 << llendl;
  437. --mShaderLevel;
  438. return createShader(attributes, uniforms);
  439. }
  440. llwarns << "Failed to link shader: " << mName << llendl;
  441. unloadInternal();
  442. return false;
  443. }
  444. if (mFeatures.mIndexedTextureChannels > 0)
  445. {
  446. // Override texture channels for indexed texture rendering
  447. bind();
  448. S32 channel_count = mFeatures.mIndexedTextureChannels;
  449. for (S32 i = 0; i < channel_count; ++i)
  450. {
  451. LLStaticHashedString uni_name(llformat("tex%d", i));
  452. uniform1i(uni_name, i);
  453. }
  454. // Adjust any texture channels that might have been overwritten
  455. S32 cur_tex = channel_count;
  456. for (U32 i = 0, count = mTexture.size(); i < count; ++i)
  457. {
  458. if (mTexture[i] > -1 && mTexture[i] < channel_count)
  459. {
  460. llassert(cur_tex < gGLManager.mNumTextureImageUnits);
  461. uniform1i(i, cur_tex);
  462. mTexture[i] = cur_tex++;
  463. }
  464. }
  465. unbind();
  466. }
  467. return true;
  468. }
  469. bool LLGLSLShader::attachVertexObject(const char* object)
  470. {
  471. // NOTE: sadly, vertex shader names do not all end with "V.glsl", so only
  472. // check for contradictory naming... HB
  473. bool error = strstr(object, "F.glsl") != 0;
  474. // PBR shaders got environment/srgbF.glsl also used as a vertex shader, so
  475. // we must check for this silly exception... HB
  476. if (error && gUsePBRShaders && strstr(object, "srgbF.glsl"))
  477. {
  478. error = false;
  479. }
  480. if (error)
  481. {
  482. llerrs << "Passing a vertex shader name for a fragment shader: "
  483. << object << llendl;
  484. }
  485. LL_DEBUGS("ShaderLoading") << "Attaching: " << object << LL_ENDL;
  486. LLShaderMgr::map_citer_t it =
  487. LLShaderMgr::sVertexShaderObjects.find(object);
  488. if (it != LLShaderMgr::sVertexShaderObjects.end())
  489. {
  490. stop_glerror();
  491. glAttachShader(mProgramObject, it->second);
  492. stop_glerror();
  493. return true;
  494. }
  495. llwarns << "Attempting to attach shader object that has not been compiled: "
  496. << object << llendl;
  497. return false;
  498. }
  499. bool LLGLSLShader::attachFragmentObject(const char* object)
  500. {
  501. // NOTE: sadly, fragment shader names do not all end with "F.glsl", so only
  502. // check for contradictory naming... HB
  503. if (strstr(object, "V.glsl"))
  504. {
  505. llerrs << "Passing a fragment shader name for a vertex shader: "
  506. << object << llendl;
  507. }
  508. LL_DEBUGS("ShaderLoading") << "Attaching: " << object << LL_ENDL;
  509. LLShaderMgr::map_citer_t it =
  510. LLShaderMgr::sFragmentShaderObjects.find(object);
  511. if (it != LLShaderMgr::sFragmentShaderObjects.end())
  512. {
  513. stop_glerror();
  514. glAttachShader(mProgramObject, it->second);
  515. stop_glerror();
  516. return true;
  517. }
  518. llwarns << "Attempting to attach shader object that has not been compiled: "
  519. << object << llendl;
  520. return false;
  521. }
  522. void LLGLSLShader::attachObject(GLuint object)
  523. {
  524. if (!object)
  525. {
  526. llwarns << "Attempting to attach non existing shader object."
  527. << llendl;
  528. return;
  529. }
  530. stop_glerror();
  531. glAttachShader(mProgramObject, object);
  532. stop_glerror();
  533. }
  534. void LLGLSLShader::attachObjects(GLuint* objects, S32 count)
  535. {
  536. for (S32 i = 0; i < count; ++i)
  537. {
  538. attachObject(objects[i]);
  539. }
  540. }
  541. bool LLGLSLShader::mapAttributes(const hash_vector_t* attributes)
  542. {
  543. // Before linking, make sure reserved attributes always have consistent
  544. // locations
  545. for (U32 i = 0, count = LLShaderMgr::sReservedAttribs.size();
  546. i < count; ++i)
  547. {
  548. const char* name = LLShaderMgr::sReservedAttribs[i].c_str();
  549. glBindAttribLocation(mProgramObject, i, (const GLchar*)name);
  550. }
  551. // Link the program
  552. bool res = LLShaderMgr::getInstance()->linkProgramObject(mProgramObject,
  553. false);
  554. mAttribute.clear();
  555. U32 num_attrs = attributes ? attributes->size() : 0;
  556. mAttribute.resize(LLShaderMgr::sReservedAttribs.size() + num_attrs, -1);
  557. // Read back channel locations
  558. if (res)
  559. {
  560. mAttributeMask = 0;
  561. // Read back reserved channels first
  562. for (U32 i = 0, count = LLShaderMgr::sReservedAttribs.size(); i < count;
  563. ++i)
  564. {
  565. const char* name = LLShaderMgr::sReservedAttribs[i].c_str();
  566. S32 index = glGetAttribLocation(mProgramObject,
  567. (const GLchar*)name);
  568. if (index != -1)
  569. {
  570. mAttribute[i] = index;
  571. mAttributeMask |= 1 << i;
  572. LL_DEBUGS("ShaderLoading") << "Attribute " << name
  573. << " assigned to channel " << index
  574. << LL_ENDL;
  575. }
  576. }
  577. if (attributes)
  578. {
  579. U32 size = LLShaderMgr::sReservedAttribs.size();
  580. for (U32 i = 0; i < num_attrs; ++i)
  581. {
  582. const char* name = (*attributes)[i].String().c_str();
  583. S32 index = glGetAttribLocation(mProgramObject, name);
  584. if (index != -1)
  585. {
  586. mAttribute[size + i] = index;
  587. LL_DEBUGS("ShaderLoading") << "Attribute " << name
  588. << " assigned to channel "
  589. << index << LL_ENDL;
  590. }
  591. }
  592. }
  593. }
  594. return res;
  595. }
  596. void LLGLSLShader::mapUniform(S32 index, const hash_vector_t* uniforms)
  597. {
  598. if (index == -1)
  599. {
  600. return;
  601. }
  602. GLenum type;
  603. GLsizei length;
  604. S32 size = -1;
  605. char name[1024];
  606. name[0] = '\0';
  607. glGetActiveUniform(mProgramObject, index, 1024, &length, &size, &type,
  608. (GLchar*)name);
  609. if (size > 0)
  610. {
  611. switch (type)
  612. {
  613. case GL_FLOAT_VEC2: size *= 2; break;
  614. case GL_FLOAT_VEC3: size *= 3; break;
  615. case GL_FLOAT_VEC4: size *= 4; break;
  616. case GL_DOUBLE: size *= 2; break;
  617. case GL_DOUBLE_VEC2: size *= 2; break;
  618. case GL_DOUBLE_VEC3: size *= 6; break;
  619. case GL_DOUBLE_VEC4: size *= 8; break;
  620. case GL_INT_VEC2: size *= 2; break;
  621. case GL_INT_VEC3: size *= 3; break;
  622. case GL_INT_VEC4: size *= 4; break;
  623. case GL_UNSIGNED_INT_VEC2: size *= 2; break;
  624. case GL_UNSIGNED_INT_VEC3: size *= 3; break;
  625. case GL_UNSIGNED_INT_VEC4: size *= 4; break;
  626. case GL_BOOL_VEC2: size *= 2; break;
  627. case GL_BOOL_VEC3: size *= 3; break;
  628. case GL_BOOL_VEC4: size *= 4; break;
  629. case GL_FLOAT_MAT2: size *= 4; break;
  630. case GL_FLOAT_MAT3: size *= 9; break;
  631. case GL_FLOAT_MAT4: size *= 16; break;
  632. case GL_FLOAT_MAT2x3: size *= 6; break;
  633. case GL_FLOAT_MAT2x4: size *= 8; break;
  634. case GL_FLOAT_MAT3x2: size *= 6; break;
  635. case GL_FLOAT_MAT3x4: size *= 12; break;
  636. case GL_FLOAT_MAT4x2: size *= 8; break;
  637. case GL_FLOAT_MAT4x3: size *= 12; break;
  638. case GL_DOUBLE_MAT2: size *= 8; break;
  639. case GL_DOUBLE_MAT3: size *= 18; break;
  640. case GL_DOUBLE_MAT4: size *= 32; break;
  641. case GL_DOUBLE_MAT2x3: size *= 12; break;
  642. case GL_DOUBLE_MAT2x4: size *= 16; break;
  643. case GL_DOUBLE_MAT3x2: size *= 12; break;
  644. case GL_DOUBLE_MAT3x4: size *= 24; break;
  645. case GL_DOUBLE_MAT4x2: size *= 16; break;
  646. case GL_DOUBLE_MAT4x3: size *= 24; break;
  647. }
  648. mTotalUniformSize += size;
  649. }
  650. S32 location = glGetUniformLocation(mProgramObject, name);
  651. if (location == -1)
  652. {
  653. return; // Not found. Nothing more to do.
  654. }
  655. // Chop off "[0]" so we can always access the first element of an array by
  656. // the array name
  657. char* is_array = strstr(name, "[0]");
  658. if (is_array)
  659. {
  660. is_array[0] = '\0';
  661. }
  662. LLStaticHashedString hashed_name(name);
  663. mUniformNameMap[location] = name;
  664. mUniformMap[hashed_name] = location;
  665. LL_DEBUGS("ShaderLoading") << "Uniform " << name << " is at location "
  666. << location << LL_ENDL;
  667. // Find the index of this uniform
  668. U32 count = LLShaderMgr::sReservedUniforms.size();
  669. for (U32 i = 0; i < count; ++i)
  670. {
  671. if (mUniform[i] == -1 && LLShaderMgr::sReservedUniforms[i] == name)
  672. {
  673. // Found it
  674. mUniform[i] = location;
  675. mTexture[i] = mapUniformTextureChannel(location, type, size);
  676. return;
  677. }
  678. }
  679. if (!uniforms)
  680. {
  681. return;
  682. }
  683. for (U32 i = 0; i < uniforms->size(); ++i)
  684. {
  685. if (mUniform[i + count] == -1 && (*uniforms)[i].String() == name)
  686. {
  687. // Found it
  688. mUniform[i + count] = location;
  689. mTexture[i + count] = mapUniformTextureChannel(location, type,
  690. size);
  691. return;
  692. }
  693. }
  694. }
  695. void LLGLSLShader::addConstant(EShaderConsts shader_const)
  696. {
  697. mDefines[sShaderConstsKey[shader_const]] = sShaderConstsVal[shader_const];
  698. }
  699. void LLGLSLShader::addPermutation(const std::string& name,
  700. const std::string& value)
  701. {
  702. mDefines.emplace(name, value);
  703. }
  704. void LLGLSLShader::addPermutations(const defines_map_t& defines)
  705. {
  706. for (defines_map_t::const_iterator it = defines.begin(),
  707. end = defines.end();
  708. it != end; ++it)
  709. {
  710. mDefines.emplace(it->first, it->second);
  711. }
  712. }
  713. S32 LLGLSLShader::mapUniformTextureChannel(S32 location, U32 type, S32 size)
  714. {
  715. if (type != GL_SAMPLER_2D_MULTISAMPLE &&
  716. type != GL_SAMPLER_CUBE_MAP_ARRAY &&
  717. (type < GL_SAMPLER_1D || type > GL_SAMPLER_2D_RECT_SHADOW))
  718. {
  719. return -1;
  720. }
  721. S32 ret = mActiveTextureChannels;
  722. if (size <= 1)
  723. {
  724. // This is a texture
  725. glUniform1i(location, mActiveTextureChannels);
  726. LL_DEBUGS("ShaderLoading") << "Location " << location << " of type "
  727. << type << " assigned to texture channel "
  728. << mActiveTextureChannels << LL_ENDL;
  729. ++mActiveTextureChannels;
  730. }
  731. else
  732. {
  733. // This is an array of textures: make sequential after this texture
  734. GLint channel[32]; // Only support up to 32 texture channels
  735. if (size > 32)
  736. {
  737. llwarns << "Too many channels (max is 32): " << size << llendl;
  738. llassert(false);
  739. size = 32;
  740. }
  741. for (S32 i = 0; i < size; ++i)
  742. {
  743. channel[i] = mActiveTextureChannels++;
  744. }
  745. glUniform1iv(location, size, channel);
  746. LL_DEBUGS("ShaderLoading") << "Assigned to texture channel "
  747. << mActiveTextureChannels - size
  748. << mActiveTextureChannels - 1 << LL_ENDL;
  749. }
  750. if (mActiveTextureChannels > 32)
  751. {
  752. llwarns << "Too many total texture channels (max is 32): "
  753. << mActiveTextureChannels << llendl;
  754. llassert(false);
  755. }
  756. return ret;
  757. }
  758. bool LLGLSLShader::mapUniforms(const hash_vector_t* uniforms)
  759. {
  760. mTotalUniformSize = 0;
  761. mActiveTextureChannels = 0;
  762. mUniform.clear();
  763. mUniformMap.clear();
  764. mUniformNameMap.clear();
  765. mTexture.clear();
  766. mValue.clear();
  767. // Initialize arrays
  768. U32 num_uniforms = uniforms ? uniforms->size() : 0;
  769. U32 size = num_uniforms + LLShaderMgr::sReservedUniforms.size();
  770. mUniform.resize(size, -1);
  771. mTexture.resize(size, -1);
  772. bind();
  773. // Get the number of active uniforms
  774. GLint active_count;
  775. glGetProgramiv(mProgramObject, GL_ACTIVE_UNIFORMS, &active_count);
  776. // This is part of code is temporary because as the final result the
  777. // mapUniform() should be rewritten. But it would need a lot of work to
  778. // avoid possible regressions.
  779. // The reason of this code is that SL engine is very sensitive to the fact
  780. // that "diffuseMap" should appear first as uniform parameter so it gains
  781. // the 0-"texture channel" index (see mapUniformTextureChannel() and
  782. // mActiveTextureChannels); it influences which texture matrix will be
  783. // updated during rendering.
  784. // The order of indexes of uniform variables is not defined and the GLSL
  785. // compilers may change it as they see fit, even if the "diffuseMap"
  786. // appears and is used first in the shader code.
  787. S32 diffuse_map = -1;
  788. S32 specular_map = -1;
  789. S32 bump_map = -1;
  790. S32 environment_map = -1;
  791. S32 altdiffuse_map = -1;
  792. S32 reflection_map = -1;
  793. static const char diffmapname[] = "diffuseMap";
  794. static const char specularmapname[] = "specularMap";
  795. static const char bumpmapname[] = "bumpMap";
  796. static const char envmapname[] = "environmentMap";
  797. static const char altdiffusename[] = "altDiffuseMap";
  798. static const char reflectionname[] = "reflectionMap";
  799. if (glGetUniformLocation(mProgramObject, diffmapname) != -1 &&
  800. (glGetUniformLocation(mProgramObject, specularmapname) != -1 ||
  801. glGetUniformLocation(mProgramObject, bumpmapname) != -1 ||
  802. glGetUniformLocation(mProgramObject, envmapname) != -1 ||
  803. glGetUniformLocation(mProgramObject, altdiffusename) != -1 ||
  804. (gUsePBRShaders &&
  805. glGetUniformLocation(mProgramObject, reflectionname) != -1)))
  806. {
  807. char name[1024];
  808. GLenum type;
  809. GLsizei length;
  810. GLint size;
  811. for (S32 i = 0; i < active_count; ++i)
  812. {
  813. name[0] = '\0';
  814. glGetActiveUniform(mProgramObject, i, 1024, &length, &size, &type,
  815. (GLchar*)name);
  816. if (diffuse_map == -1 && strcmp(name, diffmapname) == 0)
  817. {
  818. diffuse_map = i;
  819. if (specular_map != -1 && bump_map != -1 &&
  820. environment_map != -1 && altdiffuse_map != -1 &&
  821. (!gUsePBRShaders || reflection_map != -1))
  822. {
  823. break; // We are done !
  824. }
  825. }
  826. else if (specular_map == -1 && strcmp(name, specularmapname) == 0)
  827. {
  828. specular_map = i;
  829. if (diffuse_map != -1 && bump_map != -1 &&
  830. environment_map != -1 && altdiffuse_map != -1 &&
  831. (!gUsePBRShaders || reflection_map != -1))
  832. {
  833. break; // We are done !
  834. }
  835. }
  836. else if (bump_map == -1 && strcmp(name, bumpmapname) == 0)
  837. {
  838. bump_map = i;
  839. if (diffuse_map != -1 && specular_map != -1 &&
  840. environment_map != -1 && altdiffuse_map != -1 &&
  841. (!gUsePBRShaders || reflection_map != -1))
  842. {
  843. break; // We are done !
  844. }
  845. }
  846. else if (environment_map == -1 && strcmp(name, envmapname) == 0)
  847. {
  848. environment_map = i;
  849. if (diffuse_map != -1 && specular_map != -1 &&
  850. bump_map != -1 && altdiffuse_map != -1 &&
  851. (!gUsePBRShaders || reflection_map != -1))
  852. {
  853. break; // We are done !
  854. }
  855. }
  856. else if (altdiffuse_map == -1 && strcmp(name, altdiffusename) == 0)
  857. {
  858. altdiffuse_map = i;
  859. if (diffuse_map != -1 && specular_map != -1 &&
  860. bump_map != -1 && environment_map != -1 &&
  861. (!gUsePBRShaders || reflection_map != -1))
  862. {
  863. break; // We are done !
  864. }
  865. }
  866. else if (gUsePBRShaders && reflection_map == -1 &&
  867. strcmp(name, reflectionname) == 0)
  868. {
  869. reflection_map = i;
  870. if (diffuse_map != -1 && specular_map != -1 &&
  871. bump_map != -1 && environment_map != -1 &&
  872. altdiffuse_map != -1)
  873. {
  874. break; // We are done !
  875. }
  876. }
  877. }
  878. // Map uniforms in the proper order
  879. if (diffuse_map != -1)
  880. {
  881. mapUniform(diffuse_map, uniforms);
  882. }
  883. else
  884. {
  885. llwarns << "Diffuse map advertized but not found in program object "
  886. << mProgramObject << " !" << llendl;
  887. llassert(false);
  888. }
  889. if (altdiffuse_map != -1)
  890. {
  891. mapUniform(altdiffuse_map, uniforms);
  892. }
  893. if (specular_map != -1)
  894. {
  895. mapUniform(specular_map, uniforms);
  896. }
  897. if (bump_map != -1)
  898. {
  899. mapUniform(bump_map, uniforms);
  900. }
  901. if (environment_map != -1)
  902. {
  903. mapUniform(environment_map, uniforms);
  904. }
  905. if (reflection_map != -1)
  906. {
  907. mapUniform(reflection_map, uniforms);
  908. }
  909. }
  910. for (S32 i = 0; i < active_count; ++i)
  911. {
  912. if (i != specular_map && i != bump_map && i != diffuse_map &&
  913. i != environment_map && i != altdiffuse_map && i != reflection_map)
  914. {
  915. mapUniform(i, uniforms);
  916. }
  917. }
  918. if (gUsePBRShaders && mFeatures.hasReflectionProbes)
  919. {
  920. // Set up block binding, in a way supported by Apple (rather than
  921. // binding = 1 in .glsl). See slide 35 and more of:
  922. // https://docs.huihoo.com/apple/wwdc/2011/
  923. // session_420__advances_in_opengl_for_mac_os_x_lion.pdf
  924. constexpr GLuint BLOCKBINDING = 1; // Picked by us
  925. // Get the index, similar to a uniform location
  926. GLuint idx = glGetUniformBlockIndex(mProgramObject,
  927. "ReflectionProbes");
  928. if (idx != GL_INVALID_INDEX)
  929. {
  930. // Set this index to a binding index
  931. glUniformBlockBinding(mProgramObject, idx, BLOCKBINDING);
  932. }
  933. }
  934. unbind();
  935. LL_DEBUGS("ShaderLoading") << "Total Uniform Size: " << mTotalUniformSize
  936. << LL_ENDL;
  937. return true;
  938. }
  939. void LLGLSLShader::bind()
  940. {
  941. gGL.flush();
  942. if (sCurBoundShader != mProgramObject)
  943. {
  944. LLVertexBuffer::unbind();
  945. glUseProgram(mProgramObject);
  946. sCurBoundShader = mProgramObject;
  947. sCurBoundShaderPtr = this;
  948. if (gUsePBRShaders)
  949. {
  950. LLVertexBuffer::setupClientArrays(mAttributeMask);
  951. }
  952. }
  953. else if (gDebugGL)
  954. {
  955. llwarns_once << "Attempt to re-bind currently bound shader program: "
  956. << mName << ". Ignored." << llendl;
  957. }
  958. if (mUniformsDirty)
  959. {
  960. LLShaderMgr::getInstance()->updateShaderUniforms(this);
  961. mUniformsDirty = false;
  962. }
  963. }
  964. void LLGLSLShader::bind(bool rigged)
  965. {
  966. if (rigged)
  967. {
  968. if (mRiggedVariant)
  969. {
  970. mRiggedVariant->bind();
  971. return;
  972. }
  973. llwarns_once << "Shader " << mName << " is missing a rigged variant !"
  974. << llendl;
  975. }
  976. bind();
  977. }
  978. void LLGLSLShader::unbind()
  979. {
  980. gGL.flush();
  981. LLVertexBuffer::unbind();
  982. glUseProgram(0);
  983. sCurBoundShader = 0;
  984. sCurBoundShaderPtr = NULL;
  985. }
  986. S32 LLGLSLShader::getTexture(S32 line, S32 index)
  987. {
  988. if (index < 0 || index >= (S32)mTexture.size())
  989. {
  990. llwarns_once << "Texture index out of range (" << index << ")";
  991. if (gDebugGL)
  992. {
  993. llcont << " at line " << line;
  994. }
  995. if (LLGLSLShader::sCurBoundShaderPtr)
  996. {
  997. llcont << " for bound shader: "
  998. << LLGLSLShader::sCurBoundShaderPtr->mName;
  999. }
  1000. llcont << llendl;
  1001. llassert(false);
  1002. return -1;
  1003. }
  1004. return mTexture[index];
  1005. }
  1006. S32 LLGLSLShader::bindTexture(S32 uniform, LLGLTexture* texp,
  1007. LLTexUnit::eTextureType mode,
  1008. LLTexUnit::eTextureColorSpace colorspace)
  1009. {
  1010. S32 channel = getTexture(__LINE__, uniform);
  1011. if (channel >= 0)
  1012. {
  1013. LLTexUnit* unitp = gGL.getTexUnit(channel);
  1014. unitp->bindFast(texp);
  1015. unitp->setTextureColorSpace(colorspace);
  1016. }
  1017. return channel;
  1018. }
  1019. // Used by the PBR renderer only.
  1020. S32 LLGLSLShader::bindTexture(S32 uniform, LLRenderTarget* targetp, bool depth,
  1021. LLTexUnit::eTextureFilterOptions mode, U32 index)
  1022. {
  1023. S32 channel = getTexture(__LINE__, uniform);
  1024. if (channel >= 0)
  1025. {
  1026. LLTexUnit* unitp = gGL.getTexUnit(channel);
  1027. if (depth)
  1028. {
  1029. unitp->bind(targetp, true);
  1030. }
  1031. else
  1032. {
  1033. bool has_mips = mode == LLTexUnit::TFO_TRILINEAR ||
  1034. mode == LLTexUnit::TFO_ANISOTROPIC;
  1035. unitp->bindManual(targetp->getUsage(),
  1036. targetp->getTexture(index), has_mips);
  1037. }
  1038. unitp->setTextureFilteringOption(mode);
  1039. }
  1040. return channel;
  1041. }
  1042. S32 LLGLSLShader::enableTexture(S32 uniform, LLTexUnit::eTextureType mode,
  1043. LLTexUnit::eTextureColorSpace colorspace)
  1044. {
  1045. S32 channel = getTexture(__LINE__, uniform);
  1046. if (channel >= 0)
  1047. {
  1048. LLTexUnit* unitp = gGL.getTexUnit(channel);
  1049. unitp->activate();
  1050. unitp->enable(mode);
  1051. unitp->setTextureColorSpace(colorspace);
  1052. }
  1053. return channel;
  1054. }
  1055. S32 LLGLSLShader::disableTexture(S32 uniform, LLTexUnit::eTextureType mode,
  1056. LLTexUnit::eTextureColorSpace colorspace)
  1057. {
  1058. S32 channel = getTexture(__LINE__, uniform);
  1059. if (channel >= 0)
  1060. {
  1061. LLTexUnit* unitp = gGL.getTexUnit(channel);
  1062. if (unitp->getCurrType() != LLTexUnit::TT_NONE)
  1063. {
  1064. if (gDebugGL && unitp->getCurrType() != mode &&
  1065. unitp->getCurColorSpace() != colorspace)
  1066. {
  1067. llwarns_once << "Texture channel " << channel
  1068. << " texture type corrupted." << llendl;
  1069. }
  1070. unitp->disable();
  1071. }
  1072. }
  1073. return channel;
  1074. }
  1075. S32 LLGLSLShader::getUniform(S32 line, U32 index)
  1076. {
  1077. if (index >= (U32)mUniform.size())
  1078. {
  1079. llwarns_once << "Uniform index out of range (" << index << ")";
  1080. if (gDebugGL)
  1081. {
  1082. llcont << " at line " << line;
  1083. }
  1084. if (LLGLSLShader::sCurBoundShaderPtr)
  1085. {
  1086. llcont << " for bound shader: "
  1087. << LLGLSLShader::sCurBoundShaderPtr->mName;
  1088. }
  1089. llcont << llendl;
  1090. llassert(false);
  1091. return -1;
  1092. }
  1093. return mUniform[index];
  1094. }
  1095. void LLGLSLShader::uniform1i(U32 index, S32 x)
  1096. {
  1097. if (mProgramObject)
  1098. {
  1099. S32 uniform = getUniform(__LINE__, index);
  1100. if (uniform >= 0)
  1101. {
  1102. uniform_value_map_t::iterator iter = mValue.find(uniform);
  1103. if (iter == mValue.end() || iter->second.mV[0] != x)
  1104. {
  1105. glUniform1i(uniform, x);
  1106. mValue[uniform] = LLVector4(x, 0.f, 0.f, 0.f);
  1107. }
  1108. }
  1109. }
  1110. }
  1111. void LLGLSLShader::uniform1f(U32 index, F32 x)
  1112. {
  1113. if (mProgramObject)
  1114. {
  1115. S32 uniform = getUniform(__LINE__, index);
  1116. if (uniform >= 0)
  1117. {
  1118. uniform_value_map_t::iterator iter = mValue.find(uniform);
  1119. if (iter == mValue.end() || iter->second.mV[0] != x)
  1120. {
  1121. glUniform1f(uniform, x);
  1122. mValue[uniform] = LLVector4(x, 0.f, 0.f, 0.f);
  1123. }
  1124. }
  1125. }
  1126. }
  1127. void LLGLSLShader::uniform2f(U32 index, F32 x, F32 y)
  1128. {
  1129. if (mProgramObject)
  1130. {
  1131. S32 uniform = getUniform(__LINE__, index);
  1132. if (uniform >= 0)
  1133. {
  1134. uniform_value_map_t::iterator iter = mValue.find(uniform);
  1135. LLVector4 vec(x, y, 0.f, 0.f);
  1136. if (iter == mValue.end() || iter->second != vec)
  1137. {
  1138. glUniform2f(uniform, x, y);
  1139. mValue[uniform] = vec;
  1140. }
  1141. }
  1142. }
  1143. }
  1144. void LLGLSLShader::uniform3f(U32 index, F32 x, F32 y, F32 z)
  1145. {
  1146. if (mProgramObject)
  1147. {
  1148. S32 uniform = getUniform(__LINE__, index);
  1149. if (uniform >= 0)
  1150. {
  1151. uniform_value_map_t::iterator iter = mValue.find(uniform);
  1152. LLVector4 vec(x, y, z, 0.f);
  1153. if (iter == mValue.end() || iter->second != vec)
  1154. {
  1155. glUniform3f(uniform, x, y, z);
  1156. mValue[uniform] = vec;
  1157. }
  1158. }
  1159. }
  1160. }
  1161. void LLGLSLShader::uniform4f(U32 index, F32 x, F32 y, F32 z, F32 w)
  1162. {
  1163. if (mProgramObject)
  1164. {
  1165. S32 uniform = getUniform(__LINE__, index);
  1166. if (uniform >= 0)
  1167. {
  1168. uniform_value_map_t::iterator iter = mValue.find(uniform);
  1169. LLVector4 vec(x, y, z, w);
  1170. if (iter == mValue.end() || iter->second != vec)
  1171. {
  1172. glUniform4f(uniform, x, y, z, w);
  1173. mValue[uniform] = vec;
  1174. }
  1175. }
  1176. }
  1177. }
  1178. void LLGLSLShader::uniform1iv(U32 index, U32 count, const S32* v)
  1179. {
  1180. if (mProgramObject)
  1181. {
  1182. S32 uniform = getUniform(__LINE__, index);
  1183. if (uniform >= 0)
  1184. {
  1185. uniform_value_map_t::iterator iter = mValue.find(uniform);
  1186. LLVector4 vec(v[0], 0.f, 0.f, 0.f);
  1187. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1188. {
  1189. glUniform1iv(uniform, count, v);
  1190. mValue[uniform] = vec;
  1191. }
  1192. }
  1193. }
  1194. }
  1195. void LLGLSLShader::uniform4iv(U32 index, U32 count, const S32* v)
  1196. {
  1197. if (mProgramObject)
  1198. {
  1199. S32 uniform = getUniform(__LINE__, index);
  1200. if (uniform >= 0)
  1201. {
  1202. uniform_value_map_t::iterator iter = mValue.find(uniform);
  1203. LLVector4 vec(v[0], v[1], v[2], v[3]);
  1204. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1205. {
  1206. glUniform1iv(uniform, count, v);
  1207. mValue[uniform] = vec;
  1208. }
  1209. }
  1210. }
  1211. }
  1212. void LLGLSLShader::uniform1fv(U32 index, U32 count, const F32* v)
  1213. {
  1214. if (mProgramObject)
  1215. {
  1216. S32 uniform = getUniform(__LINE__, index);
  1217. if (uniform >= 0)
  1218. {
  1219. uniform_value_map_t::iterator iter = mValue.find(uniform);
  1220. LLVector4 vec(v[0], 0.f, 0.f, 0.f);
  1221. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1222. {
  1223. glUniform1fv(uniform, count, v);
  1224. mValue[uniform] = vec;
  1225. }
  1226. }
  1227. }
  1228. }
  1229. void LLGLSLShader::uniform2fv(U32 index, U32 count, const F32* v)
  1230. {
  1231. if (mProgramObject)
  1232. {
  1233. S32 uniform = getUniform(__LINE__, index);
  1234. if (uniform >= 0)
  1235. {
  1236. uniform_value_map_t::iterator iter = mValue.find(uniform);
  1237. LLVector4 vec(v[0], v[1], 0.f, 0.f);
  1238. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1239. {
  1240. glUniform2fv(uniform, count, v);
  1241. mValue[uniform] = vec;
  1242. }
  1243. }
  1244. }
  1245. }
  1246. void LLGLSLShader::uniform3fv(U32 index, U32 count, const F32* v)
  1247. {
  1248. if (mProgramObject)
  1249. {
  1250. S32 uniform = getUniform(__LINE__, index);
  1251. if (uniform >= 0)
  1252. {
  1253. uniform_value_map_t::iterator iter = mValue.find(uniform);
  1254. LLVector4 vec(v[0], v[1], v[2], 0.f);
  1255. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1256. {
  1257. glUniform3fv(uniform, count, v);
  1258. mValue[uniform] = vec;
  1259. }
  1260. }
  1261. }
  1262. }
  1263. void LLGLSLShader::uniform4fv(U32 index, U32 count, const F32* v)
  1264. {
  1265. if (mProgramObject)
  1266. {
  1267. S32 uniform = getUniform(__LINE__, index);
  1268. if (uniform >= 0)
  1269. {
  1270. uniform_value_map_t::iterator iter = mValue.find(uniform);
  1271. LLVector4 vec(v[0], v[1], v[2], v[3]);
  1272. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1273. {
  1274. glUniform4fv(uniform, count, v);
  1275. mValue[uniform] = vec;
  1276. }
  1277. }
  1278. }
  1279. }
  1280. void LLGLSLShader::uniformMatrix2fv(U32 index, U32 count, GLboolean transpose,
  1281. const F32* v)
  1282. {
  1283. if (mProgramObject)
  1284. {
  1285. S32 uniform = getUniform(__LINE__, index);
  1286. if (uniform >= 0)
  1287. {
  1288. glUniformMatrix2fv(uniform, count, transpose, v);
  1289. }
  1290. }
  1291. }
  1292. void LLGLSLShader::uniformMatrix3fv(U32 index, U32 count, GLboolean transpose,
  1293. const F32* v)
  1294. {
  1295. if (mProgramObject)
  1296. {
  1297. S32 uniform = getUniform(__LINE__, index);
  1298. if (uniform >= 0)
  1299. {
  1300. glUniformMatrix3fv(uniform, count, transpose, v);
  1301. }
  1302. }
  1303. }
  1304. void LLGLSLShader::uniformMatrix3x4fv(U32 index, U32 count,
  1305. GLboolean transpose, const F32* v)
  1306. {
  1307. if (mProgramObject)
  1308. {
  1309. S32 uniform = getUniform(__LINE__, index);
  1310. if (uniform >= 0)
  1311. {
  1312. glUniformMatrix3x4fv(uniform, count, transpose, v);
  1313. }
  1314. }
  1315. }
  1316. void LLGLSLShader::uniformMatrix4fv(U32 index, U32 count, GLboolean transpose,
  1317. const F32* v)
  1318. {
  1319. if (mProgramObject)
  1320. {
  1321. S32 uniform = getUniform(__LINE__, index);
  1322. if (uniform >= 0)
  1323. {
  1324. glUniformMatrix4fv(uniform, count, transpose, v);
  1325. }
  1326. }
  1327. }
  1328. S32 LLGLSLShader::getUniformLocation(const LLStaticHashedString& uniform)
  1329. {
  1330. if (!mProgramObject)
  1331. {
  1332. return -1;
  1333. }
  1334. LLStaticStringTable<S32>::iterator iter = mUniformMap.find(uniform);
  1335. if (iter == mUniformMap.end())
  1336. {
  1337. return -1;
  1338. }
  1339. if (gDebugGL)
  1340. {
  1341. stop_glerror();
  1342. if (iter->second != glGetUniformLocation(mProgramObject,
  1343. uniform.String().c_str()))
  1344. {
  1345. llwarns_once << "Uniform does not match: "
  1346. << uniform.String().c_str() << llendl;
  1347. }
  1348. }
  1349. return iter->second;
  1350. }
  1351. S32 LLGLSLShader::getUniformLocation(U32 index)
  1352. {
  1353. if (mProgramObject && index < mUniform.size())
  1354. {
  1355. return mUniform[index];
  1356. }
  1357. return -1;
  1358. }
  1359. S32 LLGLSLShader::getAttribLocation(U32 attrib)
  1360. {
  1361. return attrib < mAttribute.size() ? mAttribute[attrib] : -1;
  1362. }
  1363. void LLGLSLShader::uniform1i(const LLStaticHashedString& uniform, S32 v)
  1364. {
  1365. S32 location = getUniformLocation(uniform);
  1366. if (location >= 0)
  1367. {
  1368. uniform_value_map_t::iterator iter = mValue.find(location);
  1369. LLVector4 vec(v, 0.f, 0.f, 0.f);
  1370. if (iter == mValue.end() || iter->second != vec)
  1371. {
  1372. glUniform1i(location, v);
  1373. mValue[location] = vec;
  1374. }
  1375. }
  1376. }
  1377. void LLGLSLShader::uniform1iv(const LLStaticHashedString& uniform, U32 count,
  1378. const S32* v)
  1379. {
  1380. S32 location = getUniformLocation(uniform);
  1381. if (location >= 0)
  1382. {
  1383. uniform_value_map_t::iterator iter = mValue.find(location);
  1384. LLVector4 vec(v[0], 0.f, 0.f, 0.f);
  1385. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1386. {
  1387. glUniform1iv(location, count, v);
  1388. mValue[location] = vec;
  1389. }
  1390. }
  1391. }
  1392. void LLGLSLShader::uniform4iv(const LLStaticHashedString& uniform, U32 count,
  1393. const S32* v)
  1394. {
  1395. S32 location = getUniformLocation(uniform);
  1396. if (location >= 0)
  1397. {
  1398. uniform_value_map_t::iterator iter = mValue.find(location);
  1399. LLVector4 vec(v[0], v[1], v[2], v[3]);
  1400. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1401. {
  1402. glUniform4iv(location, count, v);
  1403. mValue[location] = vec;
  1404. }
  1405. }
  1406. }
  1407. void LLGLSLShader::uniform2i(const LLStaticHashedString& uniform, S32 i, S32 j)
  1408. {
  1409. S32 location = getUniformLocation(uniform);
  1410. if (location >= 0)
  1411. {
  1412. uniform_value_map_t::iterator iter = mValue.find(location);
  1413. LLVector4 vec(i, j, 0.f, 0.f);
  1414. if (iter == mValue.end() || iter->second != vec)
  1415. {
  1416. glUniform2i(location, i, j);
  1417. mValue[location] = vec;
  1418. }
  1419. }
  1420. }
  1421. void LLGLSLShader::uniform1f(const LLStaticHashedString& uniform, F32 v)
  1422. {
  1423. S32 location = getUniformLocation(uniform);
  1424. if (location >= 0)
  1425. {
  1426. uniform_value_map_t::iterator iter = mValue.find(location);
  1427. LLVector4 vec(v, 0.f, 0.f, 0.f);
  1428. if (iter == mValue.end() || iter->second != vec)
  1429. {
  1430. glUniform1f(location, v);
  1431. mValue[location] = vec;
  1432. }
  1433. }
  1434. }
  1435. void LLGLSLShader::uniform2f(const LLStaticHashedString& uniform, F32 x, F32 y)
  1436. {
  1437. S32 location = getUniformLocation(uniform);
  1438. if (location >= 0)
  1439. {
  1440. uniform_value_map_t::iterator iter = mValue.find(location);
  1441. LLVector4 vec(x, y, 0.f, 0.f);
  1442. if (iter == mValue.end() || iter->second != vec)
  1443. {
  1444. glUniform2f(location, x,y);
  1445. mValue[location] = vec;
  1446. }
  1447. }
  1448. }
  1449. void LLGLSLShader::uniform3f(const LLStaticHashedString& uniform, F32 x,
  1450. F32 y, F32 z)
  1451. {
  1452. S32 location = getUniformLocation(uniform);
  1453. if (location >= 0)
  1454. {
  1455. uniform_value_map_t::iterator iter = mValue.find(location);
  1456. LLVector4 vec(x, y, z, 0.f);
  1457. if (iter == mValue.end() || iter->second != vec)
  1458. {
  1459. glUniform3f(location, x, y, z);
  1460. mValue[location] = vec;
  1461. }
  1462. }
  1463. }
  1464. void LLGLSLShader::uniform1fv(const LLStaticHashedString& uniform, U32 count,
  1465. const F32* v)
  1466. {
  1467. S32 location = getUniformLocation(uniform);
  1468. if (location >= 0)
  1469. {
  1470. uniform_value_map_t::iterator iter = mValue.find(location);
  1471. LLVector4 vec(v[0], 0.f, 0.f, 0.f);
  1472. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1473. {
  1474. glUniform1fv(location, count, v);
  1475. mValue[location] = vec;
  1476. }
  1477. }
  1478. }
  1479. void LLGLSLShader::uniform2fv(const LLStaticHashedString& uniform, U32 count,
  1480. const F32* v)
  1481. {
  1482. S32 location = getUniformLocation(uniform);
  1483. if (location >= 0)
  1484. {
  1485. uniform_value_map_t::iterator iter = mValue.find(location);
  1486. LLVector4 vec(v[0], v[1], 0.f, 0.f);
  1487. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1488. {
  1489. glUniform2fv(location, count, v);
  1490. mValue[location] = vec;
  1491. }
  1492. }
  1493. }
  1494. void LLGLSLShader::uniform3fv(const LLStaticHashedString& uniform, U32 count,
  1495. const F32* v)
  1496. {
  1497. S32 location = getUniformLocation(uniform);
  1498. if (location >= 0)
  1499. {
  1500. uniform_value_map_t::iterator iter = mValue.find(location);
  1501. LLVector4 vec(v[0], v[1], v[2], 0.f);
  1502. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1503. {
  1504. glUniform3fv(location, count, v);
  1505. mValue[location] = vec;
  1506. }
  1507. }
  1508. }
  1509. void LLGLSLShader::uniform4fv(const LLStaticHashedString& uniform, U32 count,
  1510. const F32* v)
  1511. {
  1512. S32 location = getUniformLocation(uniform);
  1513. if (location >= 0)
  1514. {
  1515. LLVector4 vec(v);
  1516. uniform_value_map_t::iterator iter = mValue.find(location);
  1517. if (count != 1 || iter == mValue.end() || iter->second != vec)
  1518. {
  1519. glUniform4fv(location, count, v);
  1520. mValue[location] = vec;
  1521. }
  1522. }
  1523. }
  1524. void LLGLSLShader::uniformMatrix4fv(const LLStaticHashedString& uniform,
  1525. U32 count, GLboolean transpose,
  1526. const F32* v)
  1527. {
  1528. S32 location = getUniformLocation(uniform);
  1529. if (location >= 0)
  1530. {
  1531. stop_glerror();
  1532. glUniformMatrix4fv(location, count, transpose, v);
  1533. stop_glerror();
  1534. }
  1535. }
  1536. void LLGLSLShader::vertexAttrib4f(U32 index, F32 x, F32 y, F32 z, F32 w)
  1537. {
  1538. if (mAttribute[index] > 0)
  1539. {
  1540. glVertexAttrib4f(mAttribute[index], x, y, z, w);
  1541. }
  1542. }
  1543. void LLGLSLShader::vertexAttrib4fv(U32 index, F32* v)
  1544. {
  1545. if (mAttribute[index] > 0)
  1546. {
  1547. glVertexAttrib4fv(mAttribute[index], v);
  1548. }
  1549. }
  1550. void LLGLSLShader::setMinimumAlpha(F32 minimum)
  1551. {
  1552. gGL.flush();
  1553. uniform1f(LLShaderMgr::MINIMUM_ALPHA, minimum);
  1554. }
  1555. //-----------------------------------------------------------------------------
  1556. // LLShaderUniforms class
  1557. //-----------------------------------------------------------------------------
  1558. void LLShaderUniforms::apply(LLGLSLShader* shader)
  1559. {
  1560. if (!mActive)
  1561. {
  1562. return;
  1563. }
  1564. for (U32 i = 0, count = mIntegers.size(); i < count; ++i)
  1565. {
  1566. const IntSetting& uniform = mIntegers[i];
  1567. shader->uniform1i(uniform.mUniform, uniform.mValue);
  1568. }
  1569. for (U32 i = 0, count = mFloats.size(); i < count; ++i)
  1570. {
  1571. const FloatSetting& uniform = mFloats[i];
  1572. shader->uniform1f(uniform.mUniform, uniform.mValue);
  1573. }
  1574. for (U32 i = 0, count = mVectors.size(); i < count; ++i)
  1575. {
  1576. const VectorSetting& uniform = mVectors[i];
  1577. shader->uniform4fv(uniform.mUniform, 1, uniform.mValue.mV);
  1578. }
  1579. for (U32 i = 0, count = mVector3s.size(); i < count; ++i)
  1580. {
  1581. const Vector3Setting& uniform = mVector3s[i];
  1582. shader->uniform3fv(uniform.mUniform, 1, uniform.mValue.mV);
  1583. }
  1584. }